push item in sub document

断了今生、忘了曾经 提交于 2020-01-15 12:18:12

问题


I have simple collection. I want to push item in question, but it gives an exception.How can I resolve it ?

/* 1 */ my collection data

{
    "_id" : ObjectId("557e8c93a6df1a22041e0879"),
    "QuestionCount" : 2.0000000000000000,
    "Questions" : [ 
        {
            "_id" : ObjectId("557e8c9ba6df1a22041e087a"),
            "DataSource" : [],
            "DataSourceItemCount" : NumberLong(0)
        }, 
        {
            "_id" : ObjectId("557e8c9fa6df1a22041e087b"),
            "DataSource" : [],
            "DataSourceItemCount" : NumberLong(0)
        }
    ],
    "Name" : "er"
}

( id =557e8c93a6df1a22041e0879 and question id = 557e8c9fa6df1a22041e087b)

db.getCollection('forms').update({
    "_id": ObjectId("557e8c93a6df1a22041e0879"),
    "Questions._id": ObjectId("557e8c9fa6df1a22041e087b")
}, {
    "$push": {
    "Questions.DataSource": {
        "_id": ObjectId("557e8e5ea6df1a27b403ff6b"),
        "CreationDate": ISODate("2015-06-15T08:35:42.923Z"),
        "IsActive": true,
        "Text": "op",
        "Value": "op"
    }
    }
})

cannot use the part (Questions of Questions.DataSource) to traverse the element ({Questions: [ { _id: ObjectId('557e8c9ba6df1a22041e087a'), CreationDate: new Date(1434356891784), IsActive: true, Name: "uıo", IsRequired: false, QuestionType: 1, AnswerInputType: 1, AnswerValidationPattern: null, AnswerValidationMessage: null, QuestionOrder: 1, DataSource: [], DataSourceItemCount: 0 }, { _id: ObjectId('557e8c9fa6df1a22041e087b'), CreationDate: new Date(1434356895695), IsActive: true, Name: "ıu", IsRequired: false, QuestionType: 2, AnswerInputType: 1, AnswerValidationPattern: null, AnswerValidationMessage: null, QuestionOrder: 2, DataSource: [], DataSourceItemCount: 0 } ]})


回答1:


You need the positional $ operator in your update portion of the statement:

db.getCollection('forms').update(
    { 
        "_id" : ObjectId("557e8c93a6df1a22041e0879"),
        "Questions._id" : ObjectId("557e8c9fa6df1a22041e087b")
    },
    { 
        "$push" : { 
            "Questions.$.DataSource" : { 
                "_id" : ObjectId("557e8e5ea6df1a27b403ff6b"),
                "CreationDate" : ISODate("2015-06-15T08:35:42.923Z"), 
                "IsActive" : true, 
                "Text" : "op", 
                "Value" : "op" 
            }
        }
    }
)

It identifies the index of the found item you want to update.

Whilst this is okay for $push operations with a nested array structure, you will quickly find that "nested arrays" are not good for updating. The main problem here is that the "positional operator" can only match the index of the "first" or "outer" array only. So it is not possible to match the position of an inner array element and update it.

I would strongly recommend changing your structure to "flatten" as a single array which is more suited to these operations.



来源:https://stackoverflow.com/questions/30840945/push-item-in-sub-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!