Hi,
I’m trying to run an update query on my database. I want to set a field if it does not exist. If it does, I want to leave it as it is.
Running this query in MongoDB works as expected:
db.getCollection('orders').update(
{ _id: "XXX" },
[{ $set: {
"acknowledgedAt": { $cond: [ { $not: ["$acknowledgedAt"] }, new Date(), "$acknowledgedAt" ] }
}}]
)
However, I can’t seem to get this to work in Meteor. I’ve tried various things but get these errors:
Error: After filtering out keys not in the schema, your modifier is now empty
Or
Error: Expected '0' to be a modifier operator like '$set'
Using rawCollection returns an error saying that it only works on server collections (even though I am trying to update from a Method on the server side).
I have tried https://atmospherejs.com/monbro/mongodb-mapreduce-aggregation (which I could not get to work - I am assuming it is because it is old and not maintained). For the same reason, I have not tried https://github.com/meteorhacks/meteor-aggregate either.
This is my current code:
if(Meteor.isServer) {
console.log('server..')
OrdersCollection.rawCollection().aggregate([
{ $match: { _id: "XXX" } },
[{
$set: {
hasBeenAcknowledged: true,
"acknowledgedAt": { $cond: [ { $not: ["$acknowledgedAt"] }, "", "$acknowledgedAt" ] }
}
}]
])
}
Currently, this is not even updating the ‘hasBeenAcknowledged’ field.
I was using this earlier (which didn’t throw an error, but it did not update the acknowledgedAt field):
OrdersCollection.update(orderId, {
$set: {
hasBeenAcknowledged: true,
hasBeenCompleted: true,
completedAt: new Date(),
acknowledgedAt: { $cond: [ { $not: ["$acknowledgedAt"] }, "", "$acknowledgedAt" ] }
}
});
I’m probably missing something obvious, but I’m not sure what. I’d appreciate any advice.