Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 从mongo数组查询值_Mongodb_Mongodb Query_Aggregation Framework_Pymongo - Fatal编程技术网

Mongodb 从mongo数组查询值

Mongodb 从mongo数组查询值,mongodb,mongodb-query,aggregation-framework,pymongo,Mongodb,Mongodb Query,Aggregation Framework,Pymongo,我想查询mongo db的数组 样本数据 db.collections.aggregate( { $arrayToObject: { "$literal": [ { "categories.sections.fields.title": "email", "categories.sections.fields.value": "sample@sample.com"} ] } } ) 在数据中,我有多个字段数组,其中包含动态字段名称,这些名称不是强制性的,并且其位置将是随机的。我想提取所有

我想查询mongo db的数组

样本数据

db.collections.aggregate(
{ $arrayToObject: { "$literal": [
   { "categories.sections.fields.title": "email", "categories.sections.fields.value": "sample@sample.com"}
] } }
)
在数据中,我有多个字段数组,其中包含动态字段名称,这些名称不是强制性的,并且其位置将是随机的。我想提取所有字段的特定标题及其值

{ 
    "_id" : NumberInt(2), 
    "categories" : [
        {
            "sections" : [
                {
                    "fields" : [
                        {
                            "title" : "Name", 
                            "value" : "ABC"
                        }, 
                        {
                            "title" : "Gender", 
                            "value" : "Female"
                        }, 
                        {
                            "title" : "DOB", 
                            "value" : "2010-03-03T18:30:00.000Z"
                        }, 
                        {
                            "title" : "Email", 
                            "value" : "sample@sample.com"
                        }, 
                        {
                            "title" : "Work", 
                            "value" : [
                                "Business"
                            ]
                        }, 
                        {
                            "title" : "Address", 
                            "value" : "new add"
                        }
                    ]
                }, 
                {
                    "fields" : [
                        {
                            "title" : "FirstJob", 
                            "value" : "new"
                        }, 
                        {
                            "title" : "Achievments", 
                            "value" : "abc"
                        }
                    ]
                }
            ]
        }
    ]
}
{ 
    "_id" : NumberInt(6), 
    "categories" : [
        {
            "sections" : [
                {
                    "fields" : [
                        {
                            "title" : "Name", 
                            "value" : "Sample"
                        }, 
                        {
                            "title" : "Gender", 
                            "value" : "Male"
                        }, 
                        {
                            "title" : "DOB", 
                            "value" : "2018-10-22T18:30:00.000Z"
                        }, 
                        {
                            "title" : "Email", 
                            "value" : "o@o.com"
                        }, 
                        {
                            "title" : "Work", 
                            "value" : [
                                "Freelancer"
                            ]
                        }, 
                        {
                            "title" : "Address", 
                            "value" : "old add"
                        }
                    ]
                }, 
                {
                    "fields" : [
                        {
                            "title" : "FirstJob", 
                            "value" : "ddds"
                        }, 
                        {
                            "title" : "Achievments", 
                            "value" : "dsdsds"
                        }
                    ]
                }
            ]
        }
    ]
}
我想使用mongo query或pymongo查找每个id的电子邮件及其价值、成就和价值

预期产出

{ 
    "_id" : NumberInt(6), 
    "categories" : [
        {
            "sections" : [
                {
                    "fields" : [

                        {
                             "Email",  : "o@o.com"

                        }
                    ]
                }, 
                {
                    "fields" : [ 
                        {
                          "Achievments" : "dsdsds"
                        }
                    ]
                }
            ]
        }
    ]
}
我尝试对象数组,但它不是动态的

db.collections.aggregate(
{ $arrayToObject: { "$literal": [
   { "categories.sections.fields.title": "email", "categories.sections.fields.value": "sample@sample.com"}
] } }
)

使用$elemMatch,我们可以获取数组的数据

db.getCollection('transactions').find({"categories.sections.fields": {
  "$elemMatch": {
        "$and": [
            {
                "title": { "$eq": "Email"  }

            },
            {
                "title": { "$eq": "Achievments" }
            }

        ]
    }}})

谢谢你的回复,但它没有工作,它给了我所有的结果,包括电子邮件和成就。我只想从所有结果的电子邮件和成就。