Mongo - 查询,嵌入式文档不匹配,除了点符号 [英] Mongo - query, Embedded document not match except dot notation

查看:13
本文介绍了Mongo - 查询,嵌入式文档不匹配,除了点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

解决方案

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.

这篇关于Mongo - 查询,嵌入式文档不匹配,除了点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆