I have a MongoDb document with the following format
{
"_id": { "$oid": "587b5a985390100541f52d8e" },
"name": "M&G",
"type": "F",
"category": "Multi",
"data": [
{
"date": { "$date": "2014-08-03T22:00:00.000Z" },
"value": 13.172
},
{
"date": { "$date": "2014-08-04T22:00:00.000Z" },
"value": 13.133
}
],
"meta": {
"code": "103039925"
}
}
I use
var cname = // collection name
var docs= [] // array of objecs e.g. {date:<Date> value:<Number> }
var opts = { w: 1, upsert: false, returnOriginal: false };
connection.collection(cname)
.findOneAndUpdate(query, { $addToSet: {data: { $each: docs }} }, opts, function (err, res) {
// do something
});
my problem is $addToSet ensure no duplicate object is added to the set but use object equality
this object has duplicate date but is not recognized as a duplicate by $addToSet
{
"date": { "$date": "2014-08-04T22:00:00.000Z" },
"value": 0
}
my application requires that any object in the array has unique date. Is there a way to obtain that? actually if an object with duplicate date is observed it should update the value of the object