@@ -25,10 +25,128 @@ const driver = new MongoDriver({
2525});
2626```
2727
28- ## IDs and ` _id `
28+ ## Unified ID Field
2929
30- MongoDB uses ` _id ` as the primary key. ObjectQL will map the standard ` _id ` field to the ObjectQL ` _id ` field.
31- When querying, ObjectQL handles the conversion between string IDs and ` ObjectId ` automatically.
30+ ObjectQL provides a ** unified ` id ` field** across all database drivers for a consistent API experience.
31+
32+ ### How It Works
33+
34+ - ** API Level** : You always use ` id ` in your application code (queries, filters, documents)
35+ - ** Database Level** : MongoDB internally uses ` _id ` as required by MongoDB conventions
36+ - ** Automatic Mapping** : The driver transparently converts between ` id ` and ` _id `
37+
38+ ### Examples
39+
40+ ** Querying by ID:**
41+ ``` typescript
42+ // ✅ Use 'id' in queries - works consistently across all drivers
43+ const query = {
44+ filters: [[' id' , ' =' , ' 507f1f77bcf86cd799439011' ]]
45+ };
46+ const users = await app .find (' users' , query );
47+ ```
48+
49+ ** Creating Documents:**
50+ ``` typescript
51+ // ✅ Use 'id' when creating - the driver maps it to '_id' internally
52+ const newUser = await app .create (' users' , {
53+ id: ' 507f1f77bcf86cd799439011' , // Optional: specify custom ID
54+ name: ' Alice' ,
55+ email: ' alice@example.com'
56+ });
57+
58+ // Result returned with 'id' field (not '_id')
59+ console .log (newUser .id ); // '507f1f77bcf86cd799439011'
60+ ```
61+
62+ ** Finding by ID:**
63+ ``` typescript
64+ // ✅ Use 'id' parameter
65+ const user = await app .findOne (' users' , ' 507f1f77bcf86cd799439011' );
66+ console .log (user .id ); // Always 'id', never '_id'
67+ ```
68+
69+ ** Sorting by ID:**
70+ ``` typescript
71+ const query = {
72+ sort: [[' id' , ' desc' ]] // ✅ Use 'id' for sorting
73+ };
74+ const users = await app .find (' users' , query );
75+ ```
76+
77+ ** Field Projection:**
78+ ``` typescript
79+ const query = {
80+ fields: [' id' , ' name' , ' email' ] // ✅ Use 'id' to select the ID field
81+ };
82+ const users = await app .find (' users' , query );
83+ // Results contain 'id', not '_id'
84+ ```
85+
86+ ### Migration from Legacy Code
87+
88+ If you have existing code using ` _id ` , you have two options:
89+
90+ 1 . ** Recommended: Migrate to ` id ` ** for consistency across drivers:
91+
92+ ** Before (Legacy):**
93+ ``` typescript
94+ // ❌ Old way - inconsistent with SQL drivers
95+ const query = {
96+ filters: [[' _id' , ' =' , ' 507f1f77bcf86cd799439011' ]],
97+ sort: [[' _id' , ' desc' ]],
98+ fields: [' _id' , ' name' ]
99+ };
100+ ```
101+
102+ ** After (Recommended):**
103+ ``` typescript
104+ // ✅ New way - consistent across all drivers
105+ const query = {
106+ filters: [[' id' , ' =' , ' 507f1f77bcf86cd799439011' ]],
107+ sort: [[' id' , ' desc' ]],
108+ fields: [' id' , ' name' ]
109+ };
110+ ```
111+
112+ 2 . ** Backward Compatible Mode:** Continue using ` _id ` in queries (results still return ` id ` )
113+
114+ The MongoDB driver ** fully supports ` _id ` in filters, sorting, and field projections** for backward compatibility:
115+
116+ ``` typescript
117+ // ✅ This works - backward compatible
118+ const query = {
119+ filters: [[' _id' , ' =' , ' 507f1f77bcf86cd799439011' ]],
120+ sort: [[' _id' , ' desc' ]],
121+ fields: [' _id' , ' name' ]
122+ };
123+ const users = await app .find (' users' , query );
124+
125+ // Results ALWAYS use 'id' regardless of query field name
126+ console .log (users [0 ].id ); // '507f1f77bcf86cd799439011'
127+ console .log (users [0 ]._id ); // undefined
128+ ```
129+
130+ ** Key Points:**
131+ - Queries accept both ` id ` and ` _id ` (automatically mapped to MongoDB's ` _id ` )
132+ - Results always return ` id ` field (never ` _id ` )
133+ - Using ` id ` is recommended for database portability
134+
135+ ## ID Generation
136+
137+ When creating documents without specifying an ` id ` , the driver automatically generates a string ID:
138+
139+ ``` typescript
140+ const newUser = await app .create (' users' , {
141+ name: ' Bob' ,
142+ // No id specified
143+ });
144+
145+ // Driver generates a unique string ID (not ObjectId)
146+ console .log (newUser .id ); // e.g., '507f1f77bcf86cd799439011'
147+ ```
148+
149+ The generated IDs are hexadecimal strings (24 characters) that maintain MongoDB's uniqueness guarantees without using ObjectId objects.
32150
33151## Limitations
34152
0 commit comments