Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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/5/ruby-on-rails-4/2.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通过将子属性上移一级来转换文档_Mongodb_Aggregation Framework - Fatal编程技术网

MongoDB通过将子属性上移一级来转换文档

MongoDB通过将子属性上移一级来转换文档,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有以下文件 { "id" : 2, "color" : { "description" : "red" } } { "id" : 2, "color" : { "description" : "blue" } } { "id" : 2, "color" : { "description" : "purple" } } { "id" : 3, "color" : { "description" : "red" } } { "id" : 3, "color" : { "description" :

我有以下文件

{ "id" : 2, "color" : { "description" : "red" } }
{ "id" : 2, "color" : { "description" : "blue" } }
{ "id" : 2, "color" : { "description" : "purple" } }
{ "id" : 3, "color" : { "description" : "red" } }
{ "id" : 3, "color" : { "description" : "orange" } }
但我想将颜色值上移一级,如下所示:

{ "id" : 2, "color" : "red" } 
{ "id" : 2, "color" : "blue" } 
{ "id" : 2, "color" : "purple" } 
{ "id" : 3, "color" : "red" } 
{ "id" : 3, "color" : "orange" } 

有没有办法作为聚合管道的一部分来执行此操作?

您希望插入文档还是将结果作为聚合查询的一部分返回?如果以后使用聚合,您可以像下面这样做-

> db.coll1.aggregate([{$project:{id:'$id','color':'$color.description'}}])
结果是

{ "_id" : ObjectId("5a4486946c4238ae80280941"), "id" : 2, "color" : "red" }
请告诉我这是不是你要找的