简体   繁体   English

如何使MongoDB查找嵌套json作为数组返回?

[英]How to make MongoDB find nested json return as an array?

I have this data: 我有此数据:

{
    "_id" : ObjectId("5461e16ee7caf96f8f3584a2"),
    "num_marcacao" : "100",
    "sexo" : "Fêmea",
    "idade" : "20",
    "bigdata" : {
        "abortos" : [ 
            {
                "data_aborto" : "2014-11-11",
                "causa_aborto" : "Aborto causa 1"
            }, 
            {
                "data_aborto" : "2014-09-01",
                "causa_aborto" : "Aborto causa 2"
            }
        ],
        "crias" : [ 
            ObjectId("5461e16ee7caf96f8f3584a2")
         ]
     }
}
{
    "_id" : ObjectId("5461e1cae7caf96f8f3584a4"),
    "num_marcacao" : "200",
    "sexo" : "Fêmea",
    "bigdata" : {
       "crias" : [ 
           ObjectId("5461e1f3e7caf96f8f3584a5"), 
           ObjectId("5461e760e7caf96f8f3584a6")
       ]
    }
}

Using the following distinct function I get one result 使用以下独特的功能,我得到一个结果

db.animal.distinct('_id', {'bigdata.crias':{$exists:true}}

Result: 结果:

{
    "0" : ObjectId("5461e16ee7caf96f8f3584a2"),
    "1" : ObjectId("5461e1cae7caf96f8f3584a4")
}    

Now I want to get the array that is in bigdata.crias like the result of the distinct query. 现在,我想获取bigdata.crias的数组,就像非bigdata.crias查询的结果一样。 I'm trying to do like this: 我正在尝试这样做:

db.animal.find(
    {
        $and: [
            {'num_marcacao': '200'},
            {'bigdata.crias':{$exists: true}}
        ]
    },
    {
        'bigdata.crias': true,
        '_id': false
    }
)

But the result is not like the one I need. 但是结果却不像我需要的那样。 This is what it's returning: 这就是返回的内容:

{
    "bigdata" : {
        "crias" : [ 
            ObjectId("5461e1f3e7caf96f8f3584a5"), 
            ObjectId("5461e760e7caf96f8f3584a6")
        ]
    }
}

And I need 我需要

{
    "0" : ObjectId("5461e1f3e7caf96f8f3584a5"),
    "1" : ObjectId("5461e760e7caf96f8f3584a6")
} 

Anyhow. 无论如何。 MongoDB does not generally do this from either the .find() or .aggregate() methods or anything general around them. MongoDB通常不会通过.find().aggregate()方法或它们周围的任何常规方法来执行此操作。 Only the .distinct() method invokes a special form where the result given is "truly" just an array of the specified "key" to be distinct on. 只有.distinct()方法会调用一种特殊形式,其中给出的结果是“真正的”,只是指定的“键”的数组不同。

You can always "inspect" the object returned and just use the array element in the structure. 您始终可以“检查”返回的对象,而只需在结构中使用数组元素。 You can also specify a "query" argument to the .distinct() command method in the first place: 您还可以首先为.distinct()命令方法指定一个“查询”参数:

db.collection.distinct(
    "bigdata.crias",
    { 
        "bigdata.crias": { "$exists": true },
        "num_marcacao": "200"
    }
);

Where you also see your $and argument is redundant. 您还会看到$and参数是多余的。 All MongoDB query arguments are an "and" implementation by default. 默认情况下,所有MongoDB查询参数都是“和”实现。 You don't need this unless you are specifying "more than one" condition on the same "field name". 除非您在同一“字段名称”上指定“多个”条件,否则您不需要这样做。 That would result in an invalid object by breaking the basic "hash/map" "unique key" rule, and which is why and "array" is used for this form to keep it valid. 这将破坏基本的“哈希/映射”“唯一键”规则,从而导致无效对象,这就是为什么“ array”用于此表单以使其保持有效。

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

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