Ali Salehi
Ali Salehi

Reputation: 6999

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({"exams.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,

Upvotes: 1

Views: 1005

Answers (1)

Billy
Billy

Reputation: 21

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.

Upvotes: 2

Related Questions