Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 - Fatal编程技术网

Mongodb (mongo)如何获取数组中包含值和大小的文档

Mongodb (mongo)如何获取数组中包含值和大小的文档,mongodb,Mongodb,我有一个mongo系列,内容如下: { "_id" : ObjectId("59e013e83260c739f029ee21"), "createdAt" : ISODate("2017-10-13T01:16:24.653+0000"), "updatedAt" : ISODate("2017-11-11T17:13:52.956+0000"), "age" : NumberInt(34), "attributes" : [

我有一个mongo系列,内容如下:

 { 
    "_id" : ObjectId("59e013e83260c739f029ee21"), 
    "createdAt" : ISODate("2017-10-13T01:16:24.653+0000"), 
    "updatedAt" : ISODate("2017-11-11T17:13:52.956+0000"), 
    "age" : NumberInt(34), 
    "attributes" : [
        {
            "year" : "2017", 
            "contest" : [
                {
                    "name" : "Category1", 
                    "division" : "Department1"
                }, 
                {
                    "name" : "Category2", 
                    "division" : "Department1"
                }
            ]
        }, 
        {
            "year" : "2016", 
            "contest" : [ 
                {
                    "name" : "Category2", 
                    "division" : "Department1"
                }
            ]
        }, 
        {
            "year" : "2015", 
            "contest" : [
                {
                    "name" : "Category1", 
                    "division" : "Department1"
                }
            ]
        }
    ], 
    "name" : {
        "id" : NumberInt(9850214), 
        "first" : "john", 
        "last" : "afham"
    }
}
现在,我如何才能获得与名称类别1竞争超过一次或超过两次的文档数。。。等等

我尝试使用size和$gt,但无法形成正确的结果

请尝试使用size

db.foo.aggregate([
  // Start with breaking down attributes:
  {$unwind: "$attributes"}

  // Next, extract only name = Category1 from the contest array.  This will yield                   
  // an array of 0 or 1 because I am assuming that contest names WITHIN
  // the contest array are unique.   If found and we get an array of 1, turn that                   
  // into a single doc instead of an array of a single doc by taking arrayElemAt 0.                 
  // Otherwise, "x" is not set into the doc AT ALL.  All other vars in the doc
  // will go away after $project; if you want to keep them, change this to
  // $addFields:
  ,{$project: {x: {$arrayElemAt: [ {$filter: {
            input: "$attributes.contest",
            as: "z",
            cond: {$eq: [ "$$z.name", "Category1" ]}
                }}, 0 ]}
  }}

  // We split up attributes before, creating multiple docs with the same _id.  We                   
  // must now "recombine" these _id (OP said he wants # of docs with name).
  // We now have to capture all the single "x" that we created above; docs without                  
  // Category1 will have NO "x" and we don't want to include them in the count.                     
  // Also, we KNOW that name can only be Category 1 but division could vary, so                     
  // let's capture that in the $push in case we might want it:
  ,{$group: {_id: "$_id", x: {$push: "$x.division"}}}

  // One more pass to compute length of array:
  ,{$addFields: {len: {$size: "$x"}} }

  // And lastly, the filter for one time or two times or n times:
  ,{$match: {len: {$gt: 2} }}

  ]);
试试这个尺码

db.foo.aggregate([
  // Start with breaking down attributes:
  {$unwind: "$attributes"}

  // Next, extract only name = Category1 from the contest array.  This will yield                   
  // an array of 0 or 1 because I am assuming that contest names WITHIN
  // the contest array are unique.   If found and we get an array of 1, turn that                   
  // into a single doc instead of an array of a single doc by taking arrayElemAt 0.                 
  // Otherwise, "x" is not set into the doc AT ALL.  All other vars in the doc
  // will go away after $project; if you want to keep them, change this to
  // $addFields:
  ,{$project: {x: {$arrayElemAt: [ {$filter: {
            input: "$attributes.contest",
            as: "z",
            cond: {$eq: [ "$$z.name", "Category1" ]}
                }}, 0 ]}
  }}

  // We split up attributes before, creating multiple docs with the same _id.  We                   
  // must now "recombine" these _id (OP said he wants # of docs with name).
  // We now have to capture all the single "x" that we created above; docs without                  
  // Category1 will have NO "x" and we don't want to include them in the count.                     
  // Also, we KNOW that name can only be Category 1 but division could vary, so                     
  // let's capture that in the $push in case we might want it:
  ,{$group: {_id: "$_id", x: {$push: "$x.division"}}}

  // One more pass to compute length of array:
  ,{$addFields: {len: {$size: "$x"}} }

  // And lastly, the filter for one time or two times or n times:
  ,{$match: {len: {$gt: 2} }}

  ]);

首先,我们需要按属性和竞争字段展平文档。然后根据文档首字母id和比赛名称分组,计算沿途的不同比赛。最后,我们过滤结果

db.person.aggregate([
    { $unwind: "$attributes" },
    { $unwind: "$attributes.contest" },

    {$group: {
              _id: {initial_id: "$_id", contest: "$attributes.contest.name"},
              count: {$sum: 1}
             }
    },
    {$match: {$and: [{"_id.contest": "Category1"}, {"count": {$gt: 1}}]}}]);

首先,我们需要按属性和竞争字段展平文档。然后根据文档首字母id和比赛名称分组,计算沿途的不同比赛。最后,我们过滤结果

db.person.aggregate([
    { $unwind: "$attributes" },
    { $unwind: "$attributes.contest" },

    {$group: {
              _id: {initial_id: "$_id", contest: "$attributes.contest.name"},
              count: {$sum: 1}
             }
    },
    {$match: {$and: [{"_id.contest": "Category1"}, {"count": {$gt: 1}}]}}]);

假设一次
竞赛
不会多次包含相同的
名称
(例如“Category1”)值,下面是您可以做的

没有任何
展开
s将提高性能,特别是在
属性
数组中有大量条目的大型集合或数据集上

db.collection.aggregate({
    $project: {
        "numberOfOccurrences": {
            $size: { // count the number of matching contest elements
                $filter: { // get rid of all contest entries that do not contain at least one entry with name "Category1"
                    input: "$attributes",
                    cond: { $in: [ "Category1", "$$this.contest.name" ] }
                }
            }
        }
    }
}, {
    $match: { // filter the number of documents
        "numberOfOccurrences": {
            $gt: 1 // put your desired min. number of matching contest entries here
        }
    }
}, {
    $count: "numberOfDocuments" // count the number of matching documents
})

假设一次
竞赛
不会多次包含相同的
名称
(例如“Category1”)值,下面是您可以做的

没有任何
展开
s将提高性能,特别是在
属性
数组中有大量条目的大型集合或数据集上

db.collection.aggregate({
    $project: {
        "numberOfOccurrences": {
            $size: { // count the number of matching contest elements
                $filter: { // get rid of all contest entries that do not contain at least one entry with name "Category1"
                    input: "$attributes",
                    cond: { $in: [ "Category1", "$$this.contest.name" ] }
                }
            }
        }
    }
}, {
    $match: { // filter the number of documents
        "numberOfOccurrences": {
            $gt: 1 // put your desired min. number of matching contest entries here
        }
    }
}, {
    $count: "numberOfDocuments" // count the number of matching documents
})

粘贴集合中的样本数据。这个数据看起来不正确,看起来上面的数据不正确correct@astro我已经更新了json数据,它100%类似于从collection@Sam更新json数据以澄清您的需求,示例文档是否必须计数?粘贴集合中的示例数据。这个数据看起来不正确,看起来上面的数据不正确correct@astro我已经更新了json数据,它100%类似于从collection@Samjson数据被更新以澄清您的需求,示例文档是否需要计数?这里很好地使用了数组点op(this.contest.name)!这里很好的使用了数组点op(this.contest.name)!在较大的阵列情况下,双$REWIND可能是致命的。drickless在上面发布了一个$unwind免费示例。@BuzzMoschetti可能可以,drickless对数据做了一些假设,而我没有。无论如何,有很多选择是很好的。在更大的阵列情况下,双$unwind可能是致命的。drickless在上面发布了一个$unwind免费示例。@BuzzMoschetti可能可以,drickless对数据做了一些假设,而我没有。不管怎样,很好,有很多选择。