将 MongoDB 集合的子集保存到另一个集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9711529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:31:56  来源:igfitidea点击:

Save Subset of MongoDB Collection to Another Collection

mongodb

提问by SemperFly

I have a set like so

我有一套这样的

{date: 20120101}
{date: 20120103}
{date: 20120104}
{date: 20120005}
{date: 20120105}

How do I save a subset of those documents with the date '20120105' to another collection?

如何将日期为“20120105”的这些文档的子集保存到另一个集合?

i.edb.subset.save(db.full_set.find({date: "20120105"}));

IEdb.subset.save(db.full_set.find({date: "20120105"}));

回答by melan

As a newer solution I would advise to use Aggregation framework for the problem:

作为较新的解决方案,我建议使用聚合框架来解决这个问题:

db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]);

db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]);

It works about 100 times faster than forEach at least in my case. This is because the entire aggregation pipeline runs in the mongod process, whereas a solution based on find()and insert()has to send all of the documents from the server to the client and then back. This has a performance penalty, even if the server and client are on the same machine.

至少在我的情况下,它的工作速度比 forEach 快大约 100 倍。这是因为整个聚合管道都在 mongod 进程中运行,而基于find()并且insert()必须将所有文档从服务器发送到客户端然后返回的解决方案。即使服务器和客户端在同一台机器上,这也会影响性能。

回答by Eve Freeman

Here's the shell version:

这是外壳版本:

db.full_set.find({date:"20120105"}).forEach(function(doc){
   db.subset.insert(doc);
});

Note: As of MongoDB 2.6, the aggregation framework makes it possible to do this faster; see melan's answerfor details.

注意:从 MongoDB 2.6 开始,聚合框架可以更快地完成此操作;有关详细信息,请参阅melan 的回答

回答by user3116889

Actually, there is an equivalent of SQL's insert into ... select fromin MongoDB. First, you convert multiple documents into an array of documents; then you insert the array into the target collection

实际上,insert into ... select fromMongoDB 中有一个等效的 SQL 。首先,您将多个文档转换为文档数组;然后将数组插入到目标集合中

db.subset.insert(db.full_set.find({date:"20120105"}).toArray())

回答by antonimmo

The most general solution is this:

最通用的解决方案是这样的:

Make use of the aggregation (answer given by @melan):

利用聚合(@melan 给出的答案):

db.full_set.aggregate({$match:{your query here...}},{$out:"sample"})
db.sample.copyTo("subset")

This works even when there are documents in "subset" before the operation and you want to preserve those "old" documents and just insert a new subset into it.

即使在操作之前“子集中”中有文档并且您想保留那些“旧”文档并只在其中插入一个新子集,这也有效。

Care must be taken, because the copyTo()command replaces the documents with the same _id.

必须小心,因为该copyTo()命令用相同的_id.

回答by Sergio Tulentsev

There's no direct equivalent of SQL's insert into ... select from ....

没有直接等同于 SQL 的insert into ... select from ....

You have to take care of it yourself. Fetch documents of interest and save them to another collection.

你必须自己照顾它。获取感兴趣的文档并将它们保存到另一个集合中。

You can do it in the shell, but I'd use a small external script in Ruby. Something like this:

您可以在 shell 中执行此操作,但我会在 Ruby 中使用一个小的外部脚本。像这样的东西:

require 'mongo'

db = Mongo::Connection.new.db('mydb')

source = db.collection('source_collection')
target = db.collection('target_collection')

source.find(date: "20120105").each do |doc|
  target.insert doc
end

回答by Amitesh

Mongodb has aggregate along with $out operator which allow to save subset into new collection. Following are the details :

Mongodb 具有聚合以及 $out 运算符,允许将子集保存到新集合中。以下是详细信息:

$outTakes the documents returned by the aggregation pipeline and writes them to a specified collection.

$out获取聚合管道返回的文档并将它们写入指定的集合。

  • The $out operation creates a new collection in the current database if one does not already exist.
  • The collection is not visible until the aggregation completes.
  • If the aggregation fails, MongoDB does not create the collection.
  • $out 操作在当前数据库中创建一个新集合(如果一个集合尚不存在)。
  • 在聚合完成之前,该集合是不可见的。
  • 如果聚合失败,MongoDB 不会创建集合。

Syntax :

句法 :

{ $out: "<output-collection>" }

ExampleA collection books contains the following documents:

示例集合书籍包含以下文档:

{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 }
{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 }
{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 }
{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 }
{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 }

The following aggregation operation pivots the data in the books collection to have titles grouped by authors and then writes the results to the authors collection.

以下聚合操作将图书集合中的数据转换为按作者分组的标题,然后将结果写入作者集合。

db.books.aggregate( [
  { $group : { _id : "$author", books: { $push: "$title" } } },
    { $out : "authors" }
] )

After the operation, the authors collection contains the following documents:

操作后,authors 集合包含以下文档:

{ "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] }
{ "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }

In the asked question, use following query and you will get new collection named 'col_20120105' in your database

在提出的问题中,使用以下查询,您将在数据库中获得名为“col_20120105”的新集合

 db.products.aggregate([
  { $match : { date : "20120105" } },
  { $out : "col_20120105" }
]);