I get a "Field name duplication not allowed with modifiers" error while trying to update a field(s) in Mongo. An example:
> db.test.insert({test: "test1", array: [0]});
> var testFetch = db.test.findOne({test: "test1"});
> db.test.update(testFetch,
{$push: {array: 1}, //push element to end of key "array"
$pop: {array: -1} //pop element from the start of key "array"
});
Field name duplication not allowed with modifiers
Is there no way to perform this atomic operation? I don't want to be doing two separate updates for this.
There's an outstanding issue for this on Mongo's ticket system: http://jira.mongodb.org/browse/SERVER-1050
Looks like it's scheduled for this year. Your scenario is definitely a sensible scenario, but it's also tied to a bunch of edge cases. What if you $push
and $pop
on an empty array? What's expected? What do you want if you $push
and $pull
?
I don't want to be doing two separate updates for this.
I know that doing this really has "code smell", but is it a complete blocker for using this solution? Is the "double-update" going to completely destroy server performance?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With