Is it possible to change the ObjectId field name from '_id' to 'id', or something else?
I've been looking in the docs and so far haven't found anything concerning this. I'm using MongoDB with pymongo (python driver) on the server, where I'll provide JSON objects, and the js library I'm using on the client expects objects with an 'id' field.
The database will always expect or add an "_id" field, but you can actually mask this from the client in some cases.
Check out the references on SON manipulators. You can transform documents as you insert them and extract them to dynamically change the "_id" field to appear as if it is an "id" field. Be warned, however, that this will only transform the document - NOT the queries: findOne({"id": "1234567890"}) won't find anything because the "id" field doesn't really exist in the database.
You will have to decide for yourself whether this is a good idea for your use case.
I don't think so. The _id field is created automatically for every document created. You could create a different id field if you wanted to use something else like an integer value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With