1

I have two mongo collections. One we can call a template and second is instance. Every time new instance is created, rather large data field is copied from template to instance. Currently the field is retrieved from mongo db template collection in application and then sent back to db as a part of instance collection insert.

Would it be possible to somehow perform this copy on insert directly in mongo db, to avoid sending several megabytes over the network back and forth?

Kadira is reporting 3 seconds lag due to this. And documents are only going to get bigger.

I am using Meteor, but I gather that that should not influence the answer much.

Martins Untals
  • 2,128
  • 2
  • 18
  • 31

1 Answers1

0

I have done some searching and I can't really find an elegant solution for you. The two ways I can think of doing it are:

1.) Fork a process to run a mongo command to copy your template as your new instance via db.collection.copyTo().

http://eureka.ykyuen.info/2015/02/26/meteor-run-shell-command-at-server-side/ https://docs.mongodb.org/manual/reference/method/db.collection.copyTo/

Or

2.) Attempt to access the raw mongo collection rather than the minimongo collection meteor provides you with so you can use the db.collection.copyTo() functionality supplied by Mongo.

var rawCollection = Collection.rawCollection();
rawCollection.copyTo(newCollection);

Can meteor mongo driver handle $each and $position operators?

I haven't tried accessing the rawCollection to see if copyTo is available, and I also don't know if it will bring it into meteor before writing out the new collection. I'm just throwing this out here as an idea for you; hopefully someone else has a better one.

Community
  • 1
  • 1
Brett McLain
  • 2,000
  • 2
  • 14
  • 32