重塑集合中的所有文档

编程入门 行业动态 更新时间:2024-10-27 18:33:02
本文介绍了重塑集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的文档中具有以下结构:

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 $projectoperator 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.

更多推荐

重塑集合中的所有文档

本文发布于:2023-11-22 20:20:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!