问题
i am confuse. here is the example:
MongoDB Enterprise > db.employee.find()
result:
{"_id":1002,"name":"Jack","address":{"previous":"Cresent Street","current":"234,Bald Hill Street","unit":"MongoDB" } }
I try this:
db.employee.find({address:{previous: "Cresent Street"}})
result: nothing returns
Next a try this:
db.employee.find({"address.previous": "Cresent Street"})
result:
{"_id":1002,"name":"Jack","address":{"previous":"Cresent Street","current":"234,Bald Hill Street","unit":"MongoDB"}}
The question is wath is wrong with this?
i use MongoDB shell version v4.2.7 installed cmd db.version() 4.2.6 debian 10.4
thanks for your replies.
回答1:
When you Query on Embedded/Nested Documents using dotted field notation
{"address.previous": "Cresent Street"}
means find a document that containd an address
field that contains a document whose previous
field is equal to "Cresent Street"
.
When you provide a subdocument like
{address:{previous: "Cresent Street"}}
this means to find a document that contains an address
field whose content is exactly the document {previous: "Cresent Street"}
, with no additional fields. If you provide multiple fields in the subdocument, field order also matters.
Both of these queries are useful in specific scenarios, pick the one that does what you need in your situaion.
来源:https://stackoverflow.com/questions/62006773/mongo-query-embedded-document-not-match-except-dot-notation