Tuesday, August 30, 2011

Mongo: Invalid Modifier Specified{$var}

This is an exception thrown by MongoDB server when you mix the modifier "$set" with a normal variable during the update process. This is both observed in the shell and PHP Mongo. This is due probably to the difference in behavior of update that when you use "$set" would only partially update the object and the latter (using a common field/variable) would actually overwrite the entire document object which is a functionality conflict.

This is a hard to debug problem if your MongoDB driver/client wrappers are deep enough. I hope this helps you solve your problem.

e.g:

PHP Mongo

$data =array('$set'=> array('value' => 1),
             'value2' => 2);
$condition = array("id"=>123);
$mongocollection->update($condition, $data);

Mongo Shell
db.testcollection.update({"_id":ObjectId("4e5c4aa37f8b9acf1000005d"),"type":"mytpe"},{"$set":{"counters":{"views":2}},"test":),"type":"yourtype"});
  • Related Links Widget for Blogspot

No comments: