Best Way To Copy Data From One Collection To Another

1,695 views
Skip to first unread message

Lee Theobald

unread,
Feb 20, 2012, 6:16:25 AM2/20/12
to mongod...@googlegroups.com
Hi all,

Hi all.  I was wondering what's the best way to copy items from one collection to another.  As far as I can tell, I have 3 options:
  1. Copy via a loop & using db.eval() as suggested via the MongoDB FAQ.
  2. Performing a MapReduce and spitting the results out into my new collection (map of "function() { emit(this._id, this ); }", reduce of "function(key, values) { return values[0]; }")
  3. Performing a MapReduce and saving the results to my collection as part of a finalize function. 

What's the best to go for?  #1 (db.eval) is a blocking action so I'd rather not take that option.  #2 does the job but wraps the output in a value object that I don't need/want.  #3 seems like a very roundabout way of doing #1 but at least it doesn't block.

We're using MongoDB to hold all the data we use for analytics.  When a request is made to one of our websites, the data goes into a general "daily_data" collection.  At the end of the day, we copy that days data from this collection into a monthly archive table (e.g. archive_201202).  I'm trying to figure out the best way of performing this copy whilst having the least impact on the system.

Thanks for any input,
Lee

Scott Hernandez

unread,
Feb 20, 2012, 8:07:13 AM2/20/12
to mongod...@googlegroups.com
I would suggest simply writing to the daily and monthly collections at
the same time, when you collect the data. Keep them both up to date
throughout the day. Then periodically remove the data from daily as
you need, or drop the whole collection at the end of the day. A drop
will be much more efficient than removing individual documents.

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/i7T70xJ6s10J.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

Reply all
Reply to author
Forward
0 new messages