mongodb将文档从一个集合移到另一个集合

mongodb将文档从一个集合移到另一个集合

问题描述:

如何在 MongoDB 中将文档从一个集合移到另一个集合?例如:我在集合A中有很多文档,并且我想将所有1个月前的旧文档移到集合B中(这些1个月前的文档不应在集合A中).

How can documents be moved from one collection to another collection in MongoDB?? For example: I have lot of documents in collection A and I want to move all 1 month older documents to collection B (these 1 month older documents should not be in collection A).

使用汇总,我们可以复制.但是我想做的是移动文件. 可以使用什么方法移动文档?

Using aggregation we can do copy. But what I am trying to do is moving of documents. What method can be used to move documents?

更新2

请不要再对此答案表示赞同.按照 @jasongarber的回答,它在任何方面都比较好.

Please do NOT upvote this answer any more. As written @jasongarber's answer is better in any aspect.

更新

@jasongarber的答案是一种更安全的方法,应该代替我的.

This answer by @jasongarber is a safer approach and should be used instead of mine.

提供正确的信息,并且您想移动所有早于1个月的文档,并且您使用的是mongoDB 2.6,没有理由不使用批量操作,这是我所知道的最有效的执行多项操作的方式:

Provided I got you right and you want to move all documents older than 1 month, and you use mongoDB 2.6, there is no reason not to use bulk operations, which are the most efficient way of doing multiple operations I am aware of:

> var bulkInsert = db.target.initializeUnorderedBulkOp()
> var bulkRemove = db.source.initializeUnorderedBulkOp()
> var date = new Date()
> date.setMonth(date.getMonth() -1)
> db.source.find({"yourDateField":{$lt: date}}).forEach(
    function(doc){
      bulkInsert.insert(doc);
      bulkRemove.find({_id:doc._id}).removeOne();
    }
  )
> bulkInsert.execute()
> bulkRemove.execute()

这应该非常快,并且它的优点是,如果在批量插入过程中出现问题,原始数据仍然会存在.

This should be pretty fast and it has the advantage that in case something goes wrong during the bulk insert, the original data still exists.

修改

为了防止占用过多的内存,您可以在处理的每个x个文档上执行批量操作:

In order to prevent too much memory to be utilized, you can execute the bulk operation on every x docs processed:

> var bulkInsert = db.target.initializeUnorderedBulkOp()
> var bulkRemove = db.source.initializeUnorderedBulkOp()
> var x = 10000
> var counter = 0
> var date = new Date()
> date.setMonth(date.getMonth() -1)
> db.source.find({"yourDateField":{$lt: date}}).forEach(
    function(doc){
      bulkInsert.insert(doc);
      bulkRemove.find({_id:doc._id}).removeOne();
      counter ++
      if( counter % x == 0){
        bulkInsert.execute()
        bulkRemove.execute()
        bulkInsert = db.target.initializeUnorderedBulkOp()
        bulkRemove = db.source.initializeUnorderedBulkOp()
      }
    }
  )
> bulkInsert.execute()
> bulkRemove.execute()