The BSON specification states that keys must be strings, so PyMongo is right to reject this as an invalid document (and it will be regardless of what level of ObjectId was used as the key, whether at the top level or in the embedded document). This is necessary, among other reasons, so the query language can be unambiguous. Imagine that you have this document (and that it was a valid BSON document):
{ _id: ...,
"4f0cbe6d7f40d36b24a5c4d7": true,
ObjectId("4f0cbe6d7f40d36b24a5c4d7"): false
}
:
db.foo.find({"4f0cbe6d7f40d36b24a5c4d7": false})
? ObjectId? Mongo, , , ?
- , :
{ answers: [
{ answer_id: ObjectId("..."), summary: "Good answer to this question" },
{ answer_id: ObjectId("..."), summary: "Bad answer to this question" }
]
}
BSON, . answers, ; answers.answer_id, ObjectId , ( ..).