简体   繁体   English

猫鼬查询子文档

[英]Mongoose Querying SubDocuments

Hello, im trying to query some nested documents at MongoDB. 您好,我试图在MongoDB上查询一些嵌套文档。 im using Mongoose.. my documents are like 我用猫鼬..我的文件就像

[
{
    "_id": "5a2ca2227c42ad67682731d4",
    "description": "some descrition",
    "photos": [
        {
            "_id": "5a2ca22b7c42ad67682731d5",  
            "approved": false,
            "text":"good"  
        },
        {
            "_id": "5a2ca72b0a1aa173aaae07da",
            "approved": true,
            "text":"bad"
        },
        {
            "_id": "5a2cabf85a41077a2f87d4e3",    
            "approved": false,
            "text":"bad"
        }
    ]
}
]

I Want to find only photos that have the value "good", at text attribute, here is my find code: 我想在文本属性中仅找到具有“好”值的照片,这是我的查找代码:

ObjectSchema.find({
        'photos': {
            $elemMatch : {
                'text' : 'good'
            }
        }
    },function(err,result){
       console.log(result);
    });

I Wanted only to return the objct with the element with value that match my query, but when one object of this list match, the whole list of photos come with the result, not only who match the text.. How can i query it, and bring only the element who match, in this case, only the element which have "good" in text.. 我只想返回带有值与我的查询相匹配的元素的objct,但是当此列表中的一个对象匹配时,整个照片列表都会附带结果,而不仅仅是匹配文本的人。我如何查询它,并只带匹配的元素,在这种情况下,只带文本中具有“ good”的元素。

im using nodejs + mongoose 即时通讯使用nodejs +猫鼬

Thank you! 谢谢!

You are using find with only conditions, which will return entire documents. 您正在使用find只用条件,这将返回整个文档。 You can also specify a projection with find to include/exclude certain fields from documents. 您还可以指定带有find的投影,以包含/排除文档中的某些字段。 You can use the $elemMatch in the projection as follows: 您可以按以下方式在投影中使用$ elemMatch:

ObjectSchema.find({
    'photos': {
        $elemMatch : {
            'text' : 'good'
        }
    }
}, {
    'photos': {
        $elemMatch : {
            'text' : 'good'
        }
    }
}, function(err,result){
   console.log(result);
});

But beware that $elemMatch in the projection will only include the first matching array element . 但是要注意, 投影中的$ elemMatch只包含第一个匹配的array元素 That is probably not what you want based on the sample doc. 根据示例文档,这可能不是您想要的。 Instead, you can use the Aggregation Framework . 相反,您可以使用Aggregation Framework You can start with something like the following (untested) and shape the output as you prefer: 您可以从以下内容开始(未经测试),然后根据需要调整输出的形状:

ObjectSchema.aggregate(
    {$unwind: "$photos"},
    {$match: {"photos.text": "good"}},
    function(err,result){
        console.log(result);
    }
)

where the $unwind makes a separate document for each element of the array. $unwind为数组的每个元素制作一个单独的文档。

To shape the output as you describe in the comment, you need to use a $group aggregation . 要按照注释中的描述调整输出,您需要使用$ group聚合 Try adding the a $group aggregation similar to the following onto the end of pipeline: 尝试将类似于以下内容的$ group聚合添加到管道的末尾:

{$group: {_id: "$_id", photos: {$push: "$photos"}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM