Mongo - query, Embedded document not match except dot notation

坚强是说给别人听的谎言 提交于 2021-02-04 21:36:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!