The below query updateMany with aggregation pipeline works in v4.2
db.AlertPreferences.updateMany({}, [
{
$match: {
$expr: {
$or: [
{ $ne: ["$preferences.MCN-ONE", null] },
{ $ne: ["$preferences.MCN-TWO", null] },
{ $ne: ["$preferences.MCN-THREE", null] }
]
}
}
},
{
$addFields : {
preferences: {
$mergeObjects: [
{ "MCN-ONE": "$preferences.MCN-ONE" },
{ "MCN-TWO": "$preferences.MCN-TWO" },
{ "MCN-THREE": "$preferences.MCN-THREE" }
]
}
}
}
])
How to modify this query to run in v3.6 where updateMany with aggregation pipeline feature not available?
//UPDATE This is the framed query based on suggestions from @prasad_
db.collection.aggregate([
{
$match: {
$expr: {
$or: [
{ $ne: ["$preferences.MCN-ONE", null] },
{ $ne: ["$preferences.MCN-TWO", null] },
{ $ne: ["$preferences.MCN-THREE", null] }
]
}
}
}
]).forEach(doc =>db.preferences.updateOne({ _id: doc._id }, {
$set : {
preferences: {
$mergeObjects: [
{ "MCN-ONE": "$preferences.MCN-ONE" },
{ "MCN-TWO": "$preferences.MCN-TWO" },
{ "MCN-THREE": "$preferences.MCN-THREE" }
]
}
}
}))
But it is not updating the documents. Any idea what is wrong? This is follow up of post .
For example, this is the document
{
"_id" : ObjectId("5d721f5296eaaafd1df263e8"),
"assetId" : "ALL",
"createdTime" : ISODate("2019-09-06T08:56:50.065Z"),
"default" : false,
"lastUpdatedTime" : ISODate("2019-09-06T09:11:35.463Z"),
"platformFleetOrgId" : "A7A33DA4C5CE4DEFBA7E4AFA5CC9D0EE",
"platformPreferenceId" : "F015049C075E4B1E80A1BF52F5EC3A20",
"platformUserId" : "686102DBD5B241D3B2165381CBFD3FC8",
"preferences" : {
"MCN-TWO" : [
"ALERTLIST"
],
"MCN-ONE" : [
"MOBILEPUSH",
"ALERTLIST",
"WEBPUSH"
],
"MCN-THREE" : [
"ALERTLIST"
],
"CCC-ALARM" : [
"MOBILEPUSH"
],
"SSD-ALARM" : [
"MOBILEPUSH"
],
"TFT-ALARM" : [
"MOBILEPUSH",
"ALERTLIST"
],
"REC-WARN" : []
},
"solutionContext" : "mfm"
}
I am expecting following result after running the query.
{
"_id" : ObjectId("5d721f5296eaaafd1df263e8"),
"assetId" : "ALL",
"createdTime" : ISODate("2019-09-06T08:56:50.065Z"),
"default" : false,
"lastUpdatedTime" : ISODate("2019-09-06T09:11:35.463Z"),
"platformFleetOrgId" : "A7A33DA4C5CE4DEFBA7E4AFA5CC9D0EE",
"platformPreferenceId" : "F015049C075E4B1E80A1BF52F5EC3A20",
"platformUserId" : "686102DBD5B241D3B2165381CBFD3FC8",
"preferences" : {
"MCN-TWO" : [
"ALERTLIST"
],
"MCN-ONE" : [
"MOBILEPUSH",
"ALERTLIST",
"WEBPUSH"
],
"MCN-THREE" : [
"ALERTLIST"
]
},
"solutionContext" : "mfm"
}