问题描述
现有如下格式的集合
'objectiveQuestions' : [{ 'answer' : 'A', 'index' : 2, 'isCorrect' : 1, 'subjectId' : 5967 }, { 'subjectId' : 5968, 'answer' : 'A', 'isCorrect' : 0, 'index' : 3 }, ...]
现在要为每个在objectiveQuestions元素添加score字段, 并给初始值2. 请问如何构造update语句呢
问题解答
回答1:加个 multi: true
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> })
http://docs.mongodb.org/manual/reference/method/db.collection.update/
回答2:update({},{'$setOnInsert':{’score’: 2}}, True, True)

