Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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聚合中的$project pipeline?_Mongodb_Aggregation Framework - Fatal编程技术网

是否将字段添加到mongodb聚合中的$project pipeline?

是否将字段添加到mongodb聚合中的$project pipeline?,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我只想通过$project管道添加一个新字段,并让其他属性通过管道进行进一步处理,我知道您可以这样做: db.people.aggregate([ {$project: {name: 1, address: 1, birth_month: {$month: "$birthdate"}}} ]) 但是随着文档越来越复杂,我很难在$project中编写20++字段名。我是否可以通过聚合管道添加一个字段,这样我就不必逐个指定其他字段,比如 db.people.aggregate([

我只想通过
$project
管道添加一个新字段,并让其他属性通过管道进行进一步处理,我知道您可以这样做:

db.people.aggregate([
     {$project: {name: 1, address: 1, birth_month: {$month: "$birthdate"}}}
])
但是随着文档越来越复杂,我很难在
$project
中编写20++字段名。我是否可以通过聚合管道添加一个字段,这样我就不必逐个指定其他字段,比如

db.people.aggregate([
     {$appendField: {birth_month: {$month: "$birthdate"}}}
])

最后,我终于解决了这个问题。请阅读这里的旧帖子(,感谢@Johnny HK)。以下是我的解决方案(mongo 2.6或更高版本):

db.people.aggregate([
{$project:{doc:$$ROOT],出生月份:{$month:$birthdate}}

{$group:{{u id::$doc.gender}}在这种情况下,不要添加20+字段,而是将该字段放在您不需要的项目中{$project:{name:0}在这种情况下,它将选择除名称以外的所有字段。我想您误解了,我想保留原始文档,其所有字段都保持不变,但有一个附加字段(出生月份)…不用在$PROJECT中写下其他字段名称谢谢,是的,这正是我想要的。不幸的是OP没有批准它作为答案,那么我应该在这里做什么?回答我自己的问题?
db.people.aggregate([
  {$project: {doc: "$$ROOT", birth_month: {$month: "$birthdate"}}}
  {$group: {_id: "$doc.gender"} } <-- here you can use fields of the original doc via $doc
])