Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Field name duplication not allowed with modifiers" on update

Tags:

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.

like image 738
Shripad Krishna Avatar asked Jan 03 '11 13:01

Shripad Krishna


1 Answers

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?

like image 121
Gates VP Avatar answered Oct 19 '22 14:10

Gates VP