I am using Bulk Operations in mongodb. I have a situation where I need to push an object into an array field if the conditions are not met.
Here if there are no items in configs
that match itype
and prefix
, then it should push the object into the configs
. How can I do this?
//! Find and push if document found but item does not exist.
orderedBulkOP.find({
"cid": newConfig.cid,
"username": newConfig.username,
"configs": {
$elemMatch: {
"itype": newConfig.itype, "prefix": newConfig.prefix
}
}
}).update({
$push: {
"configs": {
"itype": newConfig.itype,
"prefix": newConfig.prefix,
"count": 0,
"createdAt": new Date().toISOString()
}
}
});
The schema is like this:
{
id: String,
uniqid: String,
cid: String,
username: String,
configs: [{
createdAt: Date,
itype: String,
prefix: String,
count: Number
}
]
}