Nested array update; using two $ signs in one field; MongoDB 1.8.2
I have a hierarchical data structure presenting answers for each question for each exam for each teacher stored in MongoDB like below:
db.foo.insert({name:"teacher1"}); //Done
db.foo.update({name:"teacher1"},{$push:{"exams":{name:"exam1"}}}); //Done
db.foo.update({"ex开发者_StackOverflow中文版ams.name":"exam1"},{$push:{"exams.$.questions":{name:"question1"}}}); //Done
db.foo.update({"exams.questions.name":"question1"},
{$push:{"exams.$.questions.$.answers":{name:"answer1"}}});
// Error => can't append to array using string field name [$]
I appreciate your comments,
You can't use two positional operators. As per :http://www.mongodb.org/display/DOCS/Updating :
The $ operator (by itself) means "position of the matched array item in the query". Use this to find an array member and then manipulate it.
Currently the $ operator only applies to the first matched item in the query.
精彩评论