Last active
March 5, 2016 00:40
-
-
Save sikanhe/2d81ee7997b6d075d9b1 to your computer and use it in GitHub Desktop.
bulk insert into minimongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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