Skip to content

Instantly share code, notes, and snippets.

@sikanhe
Last active March 5, 2016 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sikanhe/2d81ee7997b6d075d9b1 to your computer and use it in GitHub Desktop.
Save sikanhe/2d81ee7997b6d075d9b1 to your computer and use it in GitHub Desktop.
bulk insert into minimongo
export function insertBulk(collection, documents) {
if(collection) {
const last = _.last(documents);
_.each(documents, item => {
if(_.isObject(item)) {
if (EJSON.equals(item._id, last._id)) {
collection.upsert({ _id: last._id}, item);
}
else {
if(_.isObject(item._id)) {
//If ObjectID is used, we must use _str to get its stirng representation
//Or else .toString() will called, which will return a different type
//of mapping than .insert/.update functions
const string = item._id.toHexString();
collection._collection._docs._map[string] = item;
}
else
//Using string value as ID
collection._collection._docs._map[item._id] = item;
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment