I need to insert a mongodb document with a field id
equal to the string representation of _id
(historic reasons, don’t ask). Short of specifying my own _id, is there any way to do it in one call?
This inserts a field that’s a literal "$_id".
from pymongo import MongoClient
...
user = { "name": "ccc", "address": "Highway 37", "id": "$_id" }
result = db.users.insert_one(user)
2
Answers
I think the issue is that MongoDB assigns the
_id
after the query gets evaluated so you can try something like that:You can generate the _id yourself instead of letting mongo do it