在子文档中推送项目 [英] push item in sub document

查看:57
本文介绍了在子文档中推送项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的收藏.我想推送有问题的项目,但它给出了例外.如何解决?

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

/* 1 */我的收藏数据

/* 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和问题ID = 557e8c9fa6df1a22041e087b)

( 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"
    }
    }
})

不能使用零件(Questions of Questions.DataSource)遍历 元素({问题:[{_id: ObjectId('557e8c9ba6df1a22041e087a'),创建日期:新 Date(1434356891784),IsActive:true,名称:uıo",IsRequired:false, QuestionType:1,AnswerInputType:1,AnswerValidationPattern:null, AnswerValidationMessage:null,QuestionOrder:1,DataSource:[], DataSourceItemCount:0},{_id:ObjectId('557e8c9fa6df1a22041e087b'), CreationDate:新日期(1434356895695),IsActive:true,名称:ıu", IsRequired:否,QuestionType:2,AnswerInputType:1, AnswerValidationPattern:null,AnswerValidationMessage:null, QuestionOrder:2,DataSource:[],DataSourceItemCount:0}]})

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 } ]})

推荐答案

您在更新中需要位置$运算符语句的一部分:

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.

尽管对于具有嵌套数组结构的$push操作来说这是可以的,但您会很快发现嵌套数组"不利于更新.这里的主要问题是位置运算符"只能与第一"或外部"数组的索引匹配.因此,不可能匹配内部数组元素的位置并对其进行更新.

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.

这篇关于在子文档中推送项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆