I'm trying to implement quite typical combination of $addToSet and $inc but for some strange reason $addToSet adds multiple records with the same id (works like $push):
{ _id: 52ce27691cb76b5a60cc11f7,
news:
[ { _id: 52ce2769c2f35d3d21000018,
count: 2,
item: 52ce2769c2f35d3d21000017 },
{ item: 52ce2769c2f35d3d21000017,
_id: 52ce2769c2f35d3d21000019 } ] }
Here is the code:
this.update({ _id : interval._id },
{ $addToSet : { news : { item : item } } },
function (err) {
if (err) {
callback(err);
return;
}
this.update({ _id : interval._id, 'news.item' : item },
{ $inc : { 'news.$.count' : 1 } },
callback);
}.bind(this));
This code was called two times so two item: 52ce2769c2f35d3d21000017 was created. $inc part works well. item
and interval
are mongoose model objects.