【问题标题】:Inserting New Object Into Specific Array Index将新对象插入特定数组索引
【发布时间】:2016-03-06 16:45:59
【问题描述】:

有一个 MongoDB 集合,它是一个从 Angular 资源返回的对象数组。

[{_id: "565ee3582b8981f015494cef", button: "", reference: "", text: "", title: "", …}, 
 {_id: "565ee3582b8981f015494cf0", button: "", reference: "", text: "", title: "", …}]

我必须允许用户将一个对象插入到数组的任何索引中,并通过 Mongoose 保存到 MongoDB。

var object = {
    button: "",
    image: {},
    reference: "",
    text: "",
    title: "",
};

我了解如何将对象推送到数组的末尾,但是如何指定插入的索引?

到目前为止,考虑首先创建对象:

Slide.create(object, function(result) {
    console.log(result);
});

然后使用update方法更新数组中的位置:

【问题讨论】:

    标签: javascript angularjs mongodb mongoose mongodb-query


    【解决方案1】:
    var object = {
        button: "",
        image: {},
        reference: "",
        text: "",
        title: "",
    };
    
    arr.splice(2, 0, object);
    

    object 推送到数组中的2nd index,即它将是第三个元素。

    【讨论】:

    • 我需要将此对象更新为 MongoDB... 将澄清问题。 TY
    【解决方案2】:

    假设您的收藏中有以下文档

    {
            "_id" : ObjectId("565eed81abab97411fbe32fc"),
            "docs" : [
                    {
                            "_id" : "565ee3582b8981f015494cef",
                            "button" : "",
                            "reference" : "",
                            "text" : "",
                            "title" : ""
                    },
                    {
                            "_id" : "565ee3582b8981f015494cf0",
                            "button" : "",
                            "reference" : "",
                            "text" : "",
                            "title" : ""
                    }
            ]
    }
    

    您需要使用$position 运算符来指定数组中$push 运算符插入元素的位置,如文档中所述:

    要使用$position 修饰符,它必须与$each 修饰符一起出现。

    演示

    var object = {
        button: "",
        image: {},
        reference: "",
        text: "",
        title: "",
    };
    
    db.slide.update({/*filter*/}, 
        { '$push': { 'docs': { '$each': [object], '$position': 1 } }
    })
    

    您新更新的文档将如下所示:

    {
            "_id" : ObjectId("565eed81abab97411fbe32fc"),
            "docs" : [
                    {
                            "_id" : "565ee3582b8981f015494cef",
                            "button" : "",
                            "reference" : "",
                            "text" : "",
                            "title" : ""
                    },
                    {
                            "button" : "",
                            "image" : {
    
                            },
                            "reference" : "",
                            "text" : "",
                            "title" : ""
                    },
                    {
                            "_id" : "565ee3582b8981f015494cf0",
                            "button" : "",
                            "reference" : "",
                            "text" : "",
                            "title" : ""
                    }
            ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2021-07-08
      • 1970-01-01
      • 2017-02-11
      • 2017-11-14
      相关资源
      最近更新 更多