1

I have

{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "[email protected]",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [ 
        {
            "name" : "We",
            "arr" : [ 
                "5a26d554677475818a795f75", 
                "5a395bb0976d5304c07f7dd4"
            ]
        }, 
        {
            "name" : "sandeep",
            "arr" : [ 
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

I want to add an element inside list.arr where name = 'we' and add only if the element does not exist

how do i perform this query.

2
  • 1
    I'm also facing the same problem Commented Dec 24, 2017 at 22:45
  • 1
    you can't achieve this one with single query, because this is nested document array. Commented Dec 25, 2017 at 2:09

3 Answers 3

1

if i properly understood you question,you want to match name field with we key,and update arr only if it exists ?, you have to use elemMatch, in other to get the right document

db.test.update({ list: { $elemMatch: { name: "We" , arr: { $nin: [ "valuette" ] }} } }, {  $push: { "list.$.arr": "valuette" } } );

the $ in "list.$.arr" matches the specified index that matches name field with we value

update: to answer OP question

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

3 Comments

It is if the element inside the array exist
I want to push if the element does not exist inside the array
ok, check the new update, it solves your problem, $nin operator checks if valuette is not in the arr array, then it does the update
1

Intial database structure

> db.collection.find().pretty()
{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "[email protected]",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [
        {
            "name" : "We",
            "arr" : [
                "5a26d554677475818a795f75",
                "5a395bb0976d5304c07f7dd4"
            ]
        },
        {
            "name" : "sandeep",
            "arr" : [
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

Query executed

> db.collection.update({ list: { $elemMatch: { name: "We" , arr: {$nin: ["55555555555555555"]} } } }, {  $push: { "list.$.arr": "55555555555555555" } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

New database structure

> db.collection.find().pretty()
{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "[email protected]",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [
        {
            "name" : "We",
            "arr" : [
                "5a26d554677475818a795f75",
                "5a395bb0976d5304c07f7dd4",
                "55555555555555555"
            ]
        },
        {
            "name" : "sandeep",
            "arr" : [
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

Again same query Executed

> db.collection.update({ list: { $elemMatch: { name: "We" , arr: {$nin: ["55555555555555555"]} } } }, {  $push: { "list.$.arr": "55555555555555555" } } )
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

Checking the database structure again

> db.collection.find().pretty()
{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "[email protected]",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [
        {
            "name" : "We",
            "arr" : [
                "5a26d554677475818a795f75",
                "5a395bb0976d5304c07f7dd4",
                "55555555555555555"
            ]
        },
        {
            "name" : "sandeep",
            "arr" : [
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

No changes Found

2 Comments

@SandeepRanjan ill check it again and will update the above asap
@SandeepRanjan Please check the query and notify
0

By using this nested query I solved this problem. I'm doing it with Node JS

router.post('/prevList', (req, res) => {
  User.update({'_id':req.user.id, list: { $elemMatch: { name: req.body.name } } },{  $addToSet: { "list.$.arr": {$each: req.body.arr} } },function(err,data) {
    console.log(data);
    res.send(data)
  });
});

Here

req.body: {name:'we',arr:["5a395bb0976d5304c07f7dd4"]}

and req.user.id is the auth id.

Hope this helps you all too.

Comments

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.