mongodb使用父文档中的值更新多个子文档

时间:2014-04-04 22:55:35

标签: javascript mongodb

我正在尝试编写一个脚本来更新我的mongodb集合中的子文档,如下所示:

1) add first_name & last_name fields to each answers array
2) populate those fields with the first_name & last_name values from the parsed answers name element.

我遇到了麻烦,因为在mongo中似乎没有明确的方法。我想要的伪代码是:

db.questions.update({answers},{
$set{"answers.$.first_name":"questions.first_name","answers.$.last_name":"questions.last_name"},{"multi":"true"}}) 

以下是数据架构:

questions = {
"_id":ObjectId,
"user_id":ObjectId,
"first_name":string,
"last_name":string,
"title":string,
"profile_image_file":string,
"avatar_replacement_number":int,
"workspace_id":ObjectId,
"question":string,
"answer_count":int,
"answers": [{
    "_id":ObjectId,
    "user_id":ObjectId,
    "name":string,
    "title":string,
    "answer":string,
    "like_user_ids":[ObjectId],
    "created_datetime": {
        "d":string,
        "t":string
    },
    "is_active":boolean
 }],
 "created_datetime":{
 "d":string,
 "t":string
  },
 "is_active":boolean
}

0 个答案:

没有答案