Reputation: 674
I need mongo to accept duplicate usernames because it is possible that you might have the same username for a different site, I have my code like this.
_(data)
.forEach(function (a, key) {
jobs['insert_' + a] = function (callback) {
mongo.open(config.MONGO_DB)
.collection('usernames')
.insert({
username: a,
indic: key,
favorites: ''
}, function (err, result) {
if (err) {
return next(err);
}
callback();
});
};
}
.commit();
async.parallel(jobs, send_response);
and I only get this one line as a result
{ "_id" : ObjectId("5592724901c01a6ca6b76a6a"), "username" : "asd", "indic" : "twitch", "favorites" : "" }
and the data I am passing is:
data = {twitch: 'asd', hitbox: 'asd', dailymotion: 'asd'}
Shouldn't I have something like this?
`{ "_id" : ObjectId("5592724901c01a6ca6b76axx"), "username" : "asd", "indic" : "twitch", "favorites" : "" }
{ "_id" : ObjectId("5592724901c01a6ca6b76axx"), "username" : "asd", "hitbox" : "twitch", "favorites" : "" }
{ "_id" : ObjectId("5592724901c01a6ca6b76axx"), "username" : "asd", "dailymotion" : "twitch", "favorites" : "" }
I am using this as my async
function.
Upvotes: 0
Views: 81
Reputation: 203419
Your a
variable is the one that is duplicated, so you keep adding the same key to jobs
(namely insert_asd
).
If you don't necessarily need to refer to the specific insert actions in the results, you can make jobs
an array instead of an object:
var jobs = _(data).map(function(a, key) {
return function(callback) {
....
};
}).value();
However, it may be possible with your MongoDB driver to add the documents in one go, instead of separately. The official MongoDB driver supports arrays of documents using insertMany()
, for instance.
Upvotes: 2