1

There is a MongoDB collection, which is an array of objects being returned from an Angular Resource.

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

I have to allow the user to insert an object into any index of the array, and save to MongoDB through Mongoose.

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

I understand how to push the object to the end of the array, but how can I specify what index for the insertion?

So far, thinking of first creating the object:

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

Then using an update method to update the position in the array:

2 Answers 2

1

Suppose you have the following document in your collection

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

You need to use $position operator to specify the location in the array at which the $push operator insert elements and as mentioned in the documentation:

To use the $position modifier, it must appear with the $each modifier.

Demo

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

db.slide.update({/*filter*/}, 
    { '$push': { 'docs': { '$each': [object], '$position': 1 } }
})

You newly updated document will look like this:

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

                        },
                        "reference" : "",
                        "text" : "",
                        "title" : ""
                },
                {
                        "_id" : "565ee3582b8981f015494cf0",
                        "button" : "",
                        "reference" : "",
                        "text" : "",
                        "title" : ""
                }
        ]
}
Sign up to request clarification or add additional context in comments.

Comments

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

arr.splice(2, 0, object);

Will push the object in the array at 2nd index i.e. it will be the third element.

1 Comment

I need to update this object to MongoDB... will clarify question. TY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.