3

My intension is to push an array into the index 'jnl_articles' which is nested deep inside.How can i achieve it using a raw mongodb query. I am using Jessengers mongoDB with laravel framework.

Here is the document:

{
"_id" : ObjectId("5ca70c3c5586e920ba79df59"),
"jnl_volumes" : [
     {
        "volume_name" : 6,
        "jnl_issues" : [ 
            {
                "issue_name" : "1",
                "created_date" : "2019-04-10",
                "jnl_articles" : [],
                "issue_status" : "1"
            }, 
        ]
    }
]

}

Below is the array item that i need to push into:

[
{
    "article_name": "art1",
    "created_date": "2019-04-10",
    "article_order": 1
},
{
    "article_name": "art2",
    "created_date": "2019-04-10",
    "article_order": 2
}

]

The desired result i need to obtain is given below.

  {
    "_id" : ObjectId("5ca70c3c5586e920ba79df59"),
    "jnl_volumes" : [
     {
        "volume_name" : 6,
        "jnl_issues" : [ 
            {
                "issue_name" : "1",
                "created_date" : "2019-04-10",
                "jnl_articles" : [ 
                    {
                        "article_name" : "art1",
                        "created_date" : "2019-04-10",
                        "article_order" : 1
                    }, 
                    {
                        "article_name" : "art2",
                        "created_date" : "2019-04-10",
                        "article_order" : 2
                    }
                ],
                "issue_status" : "1"
            }, 
        ]
    }
]

}

2 Answers 2

1

Assuming you want to push articles to the first issue of the first volume, it'd look like:

{ $push: { "jnl_volumes.0.jnl_issues.0.jnl_articles": { $each: [] } } }
// use first volume ----^            ^                  ^      ^
// use first issue ------------------/                  |      |
// use $each to append an array of elements ------------/      |
// your array of articles -------------------------------------/

Documentation for $push and $each.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

await table_name.update({
    _id: ObjectId("5ca70c3c5586e920ba79df59")
}, {
    $push: {
        jnl_articles: $each: [{
            object
        }, {
            object
        }]
    }
});

Or

for (const object of array) {
    await table_name.update(
    {_id: ObjectId("5ca70c3c5586e920ba79df59")},
    {$push: { jnl_articles: object } });
}

2 Comments

Welcome to SO! Thanks for this answer! Please consider adding a little detail as to how this code works, and format the code.
If you want to push array you use the $each or loop your array then you can do push the one object. doesn't use $each method.

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.