子集数组中的mongodb add元素给出错误

时间:2015-05-27 09:00:00

标签: mongodb

我想在子集数组中添加一个元素,但它会出错。我该如何解决这个问题?

错误:

error: {
    "$err" : "Can't canonicalize query: BadValue Unsupported projection option: $push: { Questions: [ { _id: ObjectId('5565841ca6df1a168c4d8049'), CreationDate: new Date(1432716315502), IsActive: true, Name: \"uuu\", IsRequired: true, QuestionType: 1.0, AnswerInputType: 1.0, AnswerValidationPattern: null, AnswerValidationMessage: null, QuestionOrder: 0.0 } ] }",
    "code" : 17287
}

更新查询:

db.getCollection('forms').find({ "_id" : ObjectId("55657c94a6df1a155c779cd1") },

{
    "$push" : { 
        "Questions" : 
           [ {
            "_id" : ObjectId("5565841ca6df1a168c4d8049"), 
            "CreationDate" : ISODate("2015-05-27T08:45:16.502Z"), 
            "IsActive" : true,
            "Name" : "uuu", 
            "IsRequired" : true, 
            "QuestionType" : 1, 
            "AnswerInputType" : 1,
            "AnswerValidationPattern" : null,
            "AnswerValidationMessage" : null, 
            "QuestionOrder" : 0 
            } ]
    },
    "$inc" : { "QuestionCount" : 1 }  }

)

2 个答案:

答案 0 :(得分:2)

  1. find方法替换为update
  2. 使用$set代替$push添加第一个元素(或者如果可能的话,添加整个Questions数组),它将正常工作。
  3. 你需要push才能在创建数组后向数组添加新元素,而不是在数组为null时。它应该是这样的:

    db.getCollection('forms').update(
        { "_id" : ObjectId("55657c94a6df1a155c779cd2") }, 
        { "$set" : { "Questions" : [
                           {"_id":ObjectId("55659c93a6df1a22e0f93a2b"),
                            "CreationDate": ISODate("2015-05-27T10:29:39.922Z"),
                            "IsActive" : true,
                            "Name" : "ddd",
                            "IsRequired" : false,
                            "QuestionType" : 1,
                            "AnswerInputType" : 1,
                            "AnswerValidationPattern" : null,
                            "AnswerValidationMessage" : null,
                            "QuestionOrder" : 0 
                           }]
        }, 
        "$inc" : { "QuestionCount" : 1 }
      })
    

    将此数组设置为上面的值后,它将具有一个元素。然后,您可以使用push

    将更多元素添加到其中
    db.getCollection('forms').update(
       {"_id": ObjectId("55657c94a6df1a155c779cd2")},
       {"$push": 
          {"Questions":
             {"_id": ObjectId("55659c93a6df1a22e0f93a2d"),
              "CreationDate" : ISODate("2015-05-27T10:40:39.922Z"),
              "IsActive" : true,
              "Name" : "ccc",
              "IsRequired" : false,
              "QuestionType" : 1,
              "AnswerInputType" : 1,
              "AnswerValidationPattern" : null,
              "AnswerValidationMessage" : null,
              "QuestionOrder" : 0 } },
       "$inc" : { "QuestionCount" : 1 } } )
    

答案 1 :(得分:0)

我在Ruby 2.7.1和M​​ongoID 7.1.2上得到了完全相同的错误消息。

The field 'license_caches' must be an array but is of type null in document

我可以通过检查嵌入式文档是否为空来解决它:

if self.license_caches.empty?
  self.license_caches = [licenseCach]
else 
  self.license_caches.push licenseCach
end

那对我有用。希望这对收到相同错误消息的所有人有所帮助。