如何在文档中找到嵌套对象? [英] How can I find nested objects in a document?

查看:89
本文介绍了如何在文档中找到嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何查询文档中的嵌套对象.假设我有一个猫鼬模式,如下所示:

I'm trying to figure out how I can query nested objects within a document. Suppose I have a mongoose Schema as follows:

var posts = mongoose.Schema({
    title : String,
    post : String,
    comments : [{
        status : Number,
        title : String,
        comment : String
    }]
});

我想搜索特定帖子中评论的状态为1的所有评论.这是我尝试过的方法,但是不起作用:

I want to search for all comments in a particular post, where the status of the comment equals 1. This is what I have tried, but it doesn't work:

Post.find(
    {_id: req.params.id, comments: { status: 1 }},
    null,
    {},
    function (err, data) {
        if (err) return console.error(err);
        return res.json(data);
    }
);

我在做什么错了?

推荐答案

使用点表示法":

Post.find(
    {
        _id: req.params.id, 
        "commments": { "$exists": true }, 
        "comments.status": 1 
    },
    function (err, data) {
        if (err) return console.error(err);
        return res.json(data);
    }
);

您不要求查询的表单中的注释准确",并且包含状态"作为字段.

Without that you are asking for comments to be "exactly" in the form you are querying and only contain "status" as a field.

如果要返回的只是数组的匹配注释,则基本上有两个选择.您可以使用

If all you want to return are just the matching comments of an array you basically have two options.Either you use the positional $ operator in order to return only the first matching element of an array, which is it's current restriction. Or you use the aggregation framework in order to return all of the items:

Post.aggregate(
    [
        // Initial match is on only the documents that contain
        { "$match": {
            "_id": req.params.id,
            "comments": { "$exists": true },
            "comments.status": 1
        }},

        // Unwind the array to "de-normalize" as documents
        { "$unwind": "$comments" },

        // Match the "unwound" documents
        { "$match": { 
            "comments.status": 1
        }},

        // Push back as an array
        { "$group": {
            "_id": "$_id",
            "title": { "$first": "$title },
            "post": { "$first": "$post" },
            "comments": { "$push": "$comments" }
        }}
    ],
    function(err,data) {

        // work in here
})

或者在MongoDB 2.6及更高版本中,您可以使用 $ map :

Or with MongoDB 2.6 and upwards you can do that "in-place" using $map instead:

Post.aggregate(
    [
        // Initial match is on only the documents that contain
        { "$match": {
            "_id": req.params.id,
            "comments": { "$exists": true },
            "comments.status": 1
        }},

        // Project matching items only
        { "$project": {
           "title": 1,
           "post": 1,
           "comments": {
               "$setDifference": [
                   { 
                       "$map": {
                           "input": "$comments",
                           "as": "el",
                           "in": {
                               "$cond": [
                                   { "$eq": [ "$$el.status", 1 ] },
                                   "$$el",
                                   false
                               ]
                           }
                       }
                   },
                   [false]
               ]
           }
        }}
    ],
    function(err,data) {

        // work in here
})

因此,此处的区别是匹配包含您在查询中指定的条件的文档",并过滤" 仅匹配查询规范的数组成员.

So the difference here is matching the "document" that contains the conditions you specify in the query and "filtering" the array members that only match the specification of your query.

这篇关于如何在文档中找到嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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