重塑集合中的所有文档 [英] Reshape all the documents in the collection
本文介绍了重塑集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文档中具有以下结构:
I have the following structure in my documents:
{
"_id" : 1,
"item" : {
"name" : "abc",
"price" : 10,
"quantity" : 2,
"date" : ISODate("2014-03-01T08:00:00Z")
}
}
我想在此上转换每个文档:
And I want to transform each document on this:
{
"_id" : 1,
"name" : "abc",
"price" : 10,
"quantity" : 2,
"date" : ISODate("2014-03-01T08:00:00Z")
}
换句话说,删除嵌入的文档,但不删除细节!
In other words remove the embedded document but not the details!
谢谢!
推荐答案
您可以使用 aggregation
,尤其是 $project
运算符. $out
运算符可让您编写结果在另一个集合中.
You can use the aggregation
especially the $project
operator for that. The $out
operator let you write the result in another collection.
db.collection.aggregate([
{ "$project": {
"_id": "$_id",
"name": "$item.name",
"price": "$item.price",
"quantity": "$item.quantity",
"date": "$item.date"}
},
{ "$out": "collection"}
])
您的文档现在看起来像这样:
You documents now look like this:
{
"_id" : 1,
"name" : "abc",
"price" : 10,
"quantity" : 2,
"date" : ISODate("2014-03-01T08:00:00Z")
}
您还可以通过给新结果集合使用相同的名称来覆盖现有的集合.
You can also overwrite the pre-existing collection by giving the new results collection the same name but this.
这篇关于重塑集合中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文