Mongo-查询,除点号外,嵌入式文档均不匹配 [英] Mongo - query, Embedded document not match except dot notation
问题描述
我很困惑.这是示例:
MongoDB Enterprise > db.employee.find()
结果:
{"_id":1002,"name":"Jack","address":{"previous":"Cresent Street","current":"234,Bald Hill Street","unit":"MongoDB" } }
我尝试:
db.employee.find({address:{previous: "Cresent Street"}})
result: nothing returns
接下来尝试一下:
db.employee.find({"address.previous": "Cresent Street"})
结果:
{"_id":1002,"name":"Jack","address":{"previous":"Cresent Street","current":"234,Bald Hill Street","unit":"MongoDB"}}
问题是这是错的吗?
我使用已安装MongoDB Shell版本v4.2.7cmd db.version()4.2.6debian 10.4
i use MongoDB shell version v4.2.7 installed cmd db.version() 4.2.6 debian 10.4
感谢您的答复.
推荐答案
When you Query on Embedded/Nested Documents using dotted field notation
{"address.previous": "Cresent Street"}
表示找到包含 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"}}
这意味着查找包含 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屋!