具有嵌套结构的MongoDB查询 [英] MongoDB query with nested structure

查看:260
本文介绍了具有嵌套结构的MongoDB查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查询(在MongoDB中)此嵌套的json结构,以便仅获取具有位置" 值等于"currentPosition" 值的嵌套对象?

How can I query (in MongoDB) this nested json structure in order to get only the nested object which has the "position" value equal to "currentPosition" value?

{  
    "JobId": "123"
    "currentPosition" : NumberInt(18), 
    "details" : [
        {
            "position": NumberInt(18),
            "fname" : "Alexander", 
            "lname" : "A",
        },
        {
            "position": NumberInt(18),
            "fname" : "Doug", 
            "lname" : "D",
        },
        {
            "position": NumberInt(15),
            "fname" : "Bruce", 
            "lname" : "B",
        },
        {
            "position": NumberInt(10),
            "fname" : "Tom", 
            "lname" : "T",
        }
    ]
}

目前,我正在通过python代码实现此目标:获取整个文档并遍历详细信息列表,以查找位置"值等于"currentPosition"值的对象.

Currently I am achieveing this by python code: getting the entire document and looping through the details list in order to find object with "position" value equal to "currentPosition" value.

最终输出看起来像

{  
    "JobId": "123"
    "currentPosition" : NumberInt(18), 
    "details" : [
        {
                "position": NumberInt(18),
                "fname" : "Alexander", 
                "lname" : "A",
            },
            {
                "position": NumberInt(18),
                "fname" : "Doug", 
                "lname" : "D",
            }
    ]
}

推荐答案

您将需要使用聚合框架.

You will need to use the aggregation framework for this.

details需要展开,以便您可以过滤掉不必要的详细信息.

details needs to be unwinded so that you can filter out the unnecessary details.

$unwind阶段之后,您将有4个文档在管道中.在下一步中,您可以使用$match过滤掉您关心的细节.

After the $unwind stage you will have 4 documents in the pipeline. In the next stage you then use a $match to filter out the details you care about.

这意味着您将获得2个文档,这些文档具有相同的JobIdcurrentPosition,但具有不同的details

This means that as a result you will get 2 documents with the same JobId and currentPosition, but with different details

https://docs.mongodb.com/manual/reference/operator /aggregation/unwind

db.getCollection("DELETE_ME").aggregate(
    [
        {
            $unwind: {
                path : "$details",
            }
        },
        {
            $match: {
                "$expr": {"$eq": ["$details.position", "$currentPosition"]}
            }
        },
    ]
);

会返回

{ 
    "_id" : ObjectId("---"), 
    "JobId" : "123", 
    "currentPosition" : NumberInt(18), 
    "details" : {
        "position" : NumberInt(18), 
        "fname" : "Alexander", 
        "lname" : "A"
    }
}
{ 
    "_id" : ObjectId("---"), 
    "JobId" : "123", 
    "currentPosition" : NumberInt(18), 
    "details" : {
        "position" : NumberInt(18), 
        "fname" : "Doug", 
        "lname" : "D"
    }
}

这篇关于具有嵌套结构的MongoDB查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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