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 $group需要一个数组,用于_id,但$out-won';我受不了_Mongodb_Aggregation Framework - Fatal编程技术网

Mongodb $group需要一个数组,用于_id,但$out-won';我受不了

Mongodb $group需要一个数组,用于_id,但$out-won';我受不了,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我需要对数组的出现次数求和。我需要将其输出到一个集合,但当我尝试使用$out关键字时,它失败了,出现了“不能为_id使用数组” 有没有办法将\u id字段的值从group stage投射到一个新键中,并创建一个新的\u id db.djnNews_filtered.aggregate([ {$unwind:"$processed_text.headline_trigrams"}, {$group:{_id:"$processed_text.headline_trigrams","num":{$s

我需要对数组的出现次数求和。我需要将其输出到一个集合,但当我尝试使用
$out
关键字时,它失败了,出现了“不能为_id使用数组”

有没有办法将
\u id
字段的值从group stage投射到一个新键中,并创建一个新的
\u id

db.djnNews_filtered.aggregate([
{$unwind:"$processed_text.headline_trigrams"},
{$group:{_id:"$processed_text.headline_trigrams","num":{$sum:1}}},
{$sort:{"num":-1}}
])

{ "_id" : [ "Reports", "First", "Quarter" ], "num" : 279 }
{ "_id" : [ "ST", "upside", "prevails" ], "num" : 167 }
{ "_id" : [ "First", "Quarter", "Results" ], "num" : 160 }
{ "_id" : [ "Announces", "First", "Quarter" ], "num" : 155 }


db.djnNews_filtered.aggregate([
{$unwind:"$processed_text.headline_trigrams"},
{$group:{_id:"$processed_text.headline_trigrams","num":{$sum:1}}},
{$sort:{"num":-1}},
{$out:"new_collection"}
])

assert: command failed: {
    "errmsg" : "exception: insert for $out failed: { connectionId: 3, err: \"can't use an array for _id\", code: 2, n: 0, ok: 1.0 }",
    "code" : 16996,
    "ok" : 0
} : aggregate failed

在MongoDB中,不能有一个id为数组的文档

你能简单地
$project
将数组添加到另一个字段吗

db.djnNews_filtered.aggregate([
  {$unwind:"$processed_text.headline_trigrams"},
  {$group:{_id:"$processed_text.headline_trigrams","num":{$sum:1}}},
  {$sort:{"num":-1}},
  {$project: {trigram: "$_id", count: "$num"}},
  {$out:"new_collection"}
])

另外,我不确定您在将文档插入集合之前对其进行排序的目的是什么。如果这种类型只用于查看数据,然后才决定将其添加到集合中,您可能需要考虑删除该步骤。是的,排序只是为了查看数据。