搜索

首页  >  问答  >  正文

mongodb - mongo 如何更新数组的每个元素 定位符只匹配第一个

现有如下格式的集合

"objectiveQuestions" : [{
      "answer" : "A",
      "index" : 2,
      "isCorrect" : 1,
      "subjectId" : 5967
    }, {
      "subjectId" : 5968,
      "answer" : "A",
      "isCorrect" : 0,
      "index" : 3
    }, ...]

现在要为每个在objectiveQuestions元素添加score字段, 并给初始值2. 请问如何构造update语句呢

迷茫迷茫2839 天前673

全部回复(2)我来回复

  • 世界只因有你

    世界只因有你2017-04-25 09:03:49

    加个multi: true

    雷雷

    http://docs.mongodb.org/manual/reference/method/db.collection.update/

    回复
    0
  • 巴扎黑

    巴扎黑2017-04-25 09:03:49

    update({},{"$setOnInsert":{'score': 2}}, True, True)

    回复
    0
  • 取消回复