mongodb 如何查询嵌套对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16002659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:09:29  来源:igfitidea点击:

How to query nested objects?

mongodbsyntaxnestedmongodb-querybson

提问by Edmondo1984

I have a problem when querying mongoDB with nested objects notation:

使用嵌套对象表示法查询 mongoDB 时遇到问题:

db.messages.find( { headers : { From: "[email protected]" } } ).count()
0
db.messages.find( { 'headers.From': "[email protected]" }  ).count()
5

I can't see what I am doing wrong. I am expecting nested object notation to return the same result as the dot notation query. Where am I wrong?

我看不出我做错了什么。我期望嵌套对象表示法返回与点表示法查询相同的结果。我哪里错了?

回答by shx2

db.messages.find( { headers : { From: "[email protected]" } } )

db.messages.find( { headers : { From: "[email protected]" } } )

This queries for documents where headersequals{ From: ... }, i.e. contains no other fields.

这查询headers等于的文档{ From: ... },即不包含其他字段。



db.messages.find( { 'headers.From': "[email protected]" } )

db.messages.find( { 'headers.From': "[email protected]" } )

This only looks at the headers.Fromfield, not affected by other fields contained in, or missing from, headers.

这仅查看headers.From字段,不受包含在headers.



Dot-notation docs

点符号文档

回答by Edmondo1984

The two query mechanism work in different ways, as suggested in the docsat the section Subdocuments:

这两种查询机制以不同的方式工作,如Subdocuments部分的文档中所建议

When the field holds an embedded document (i.e, subdocument), you can either specify the entire subdocumentas the value of a field, or “reach into” the subdocumentusing dot notation, to specify values for individual fields in the subdocument:

当字段包含嵌入文档(即subdocument)时,您可以将整个子文档指定为一个字段的值,或者使用点表示法“进入”子文档,以指定子文档中各个字段的值:

Equality matches within subdocuments select documents if the subdocument matches exactly the specified subdocument, including the field order.

如果子文档与指定的子文档(包括字段顺序)完全匹配,则子文档中的相等匹配会选择文档。



In the following example, the query matches all documents where the value of the field producer is a subdocument that contains only the field companywith the value 'ABC123'and the field addresswith the value '123 Street', in the exact order:

在以下示例中,查询匹配字段生产者的值是仅包含company具有值'ABC123'的字段address和具有值的字段的子文档的所有文档'123 Street',按确切顺序:

db.inventory.find( {
    producer: {
        company: 'ABC123',
        address: '123 Street'
    }
});

回答by krishna Prasad

Since there is a lot of confusion about queries MongoDB collection with sub-documents, I thought its worth to explain the above answers with examples:

由于查询 MongoDB collection with sub-documents存在很多混淆,我认为值得用示例解释上述答案:

First I have inserted only two objects in the collection namely: messageas:

首先,我在集合中只插入了两个对象,即:message作为:

> db.messages.find().pretty()
{
    "_id" : ObjectId("5cce8e417d2e7b3fe9c93c32"),
    "headers" : {
        "From" : "[email protected]"
    }
}
{
    "_id" : ObjectId("5cce8eb97d2e7b3fe9c93c33"),
    "headers" : {
        "From" : "[email protected]",
        "To" : "[email protected]"
    }
}
>

So what is the result of query: db.messages.find({headers: {From: "[email protected]"} }).count()

那么查询的结果是什么: db.messages.find({headers: {From: "[email protected]"} }).count()

It should be onebecause these queries for documents where headersequal to the object {From: "[email protected]"}, only i.e. contains no other fields or we should specify the entire sub-document as the value of a field.

它应该是一个,因为这些对headers等于 object 的文档的查询{From: "[email protected]"},仅 ie 不包含其他字段,或者我们应该将整个子文档指定为字段的值。

So as per the answer from @Edmondo1984

所以根据@Edmondo1984的回答

Equality matches within sub-documents select documents if the subdocument matches exactly the specified sub-document, including the field order.

如果子文档与指定的子文档完全匹配,包括字段 order ,则子文档中的相等匹配选择文档。

From the above statements, what is the below query result should be?

从上面的语句,下面的查询结果应该是什么?

> db.messages.find({headers: {To: "[email protected]", From: "[email protected]"}  }).count()
0

And what if we will change the order of Fromand Toi.e same as sub-documents of second documents?

如果我们更改第二个文档的子文档的顺序From和顺序会To怎样?

> db.messages.find({headers: {From: "[email protected]", To: "[email protected]"}  }).count()
1

so, it matches exactly the specified sub-document, including the field order.

因此,它与指定的子文档完全匹配,包括字段 order

For using dot operator, I think it is very clear for every one. Let's see the result of below query:

对于使用点运算符,我认为每个人都非常清楚。让我们看看以下查询的结果:

> db.messages.find( { 'headers.From': "[email protected]" }  ).count()
2

I hope these explanations with the above example will make someone more clarity on find query with sub-documents.

我希望以上示例的这些解释将使人们更清楚地了解带有子文档的查找查询