- node: 0.10.13
- mongo: 2.4.1
- mongodb-native: 1.3.23
Question:
How do I insert an array of simple strings as new documents with a single call? I don't want to convert it before. I am looking for some mongo magic here. The $push
, $each
things don't seem to work for insertions.
var newTags = [];
newTags.push("tagA");
newTags.push("tagB");
// with this lodash conversion the batch insertion works fine.
// ["tagA", "tagB"] -> [{name: "tagA", name: "tagB"}]
// newTags = _.map(newTags, function(key) {return {'name': key}});
db.collection('tags').insert(newTags, {w: 1}, function (err, result) {
});
My definition of 'not working'
Mongo does insert the newTags, but creates a document from each string like this.
I understand why it does it
{
"_id": "111111116c021a165abcdd16",
"0": "t",
"1": "a",
"2": "g",
"3": "A"
},
{
"_id": "111111126c021a165abcdd17",
"0": "t",
"1": "a",
"2": "g",
"3": "B"
}
but should be
{
"_id": "111111116c021a165abcdd16",
"name": "tagA"
},
{
"_id": "111111126c021a165abcdd17",
"name": "tagB"
}