3

I am trying to find an update a document using mongoose, by removing an object from a nested array. My target document is the following:

user = {
    "userId" : "myId",
    "connections":
    [{
        "dateConnectedUnix": 1334567891,
        "isActive": true,
        "sessions": [
            {"device": "mobile", "country": "US"},
            {"device": "desktop", "country": "US"}
        ]
    }, {
        "dateConnectedUnix": 1334567893,
        "isActive": false,
        "sessions": [
            {"device": "mobile", "country": "CA"},
            {"device": "desktop", "country": "CA"}
        ]
    }]
}

here is my attempt, but it is not updating the document:

Users.findOneAndUpdate({ "userId": "myId", "connections.dateConnectedUnix": 1334567891 },
    { $pull: { sessions: { device: "mobile" } } }, (err) => {
        if (err) {
            return res.status(404).json({ message: 'Error' });
        }
        return res.status(200).json({
            success: true,
            message: 'success'
        });
    }
);

the resulting document should look like:

user = {
    "userId" : "myId",
    "connections":
    [{
        "dateConnectedUnix": 1334567891,
        "isActive": true,
        "sessions": [
            {"device": "desktop", "country": "US"}
        ]
    }, {
        "dateConnectedUnix": 1334567893,
        "isActive": false,
        "sessions": [
            {"device": "mobile", "country": "CA"},
            {"device": "desktop", "country": "CA"}
        ]
    }]
}

basically it is finding the user by the id, and then finding the connection by date, and then removing the device if mobile. In my particular case, the result is always one matching document and one matching connection and one matching session.

0

3 Answers 3

7

Because your sessions array is inside connections

Try "connections.$.sessions" instead of sessions so your query would be

Users.findOneAndUpdate({ "userId": "myId", "connections.dateConnectedUnix": 1334567891 },
    { $pull: { "connections.$.sessions" : { device: "mobile" } } }, (err) => {
        if (err) {
            return res.status(404).json({ message: 'Error' });
        }
        return res.status(200).json({
            success: true,
            message: 'success'
        });
    }
);
Sign up to request clarification or add additional context in comments.

Comments

0
Users.findOneAndUpdate(
    { "userId": "myId",  connections: { "$elemMatch": {dateConnectedUnix: 1334567891} } },
    { $pull: { "connections.$.sessions" : { device: "mobile" } } });

$elemMatch is required

ref: https://www.mongodb.com/docs/manual/reference/operator/update/positional/#update-embedded-documents-using-multiple-field-matches

1 Comment

Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2
Users.findOneAndUpdate({ _id: "myId" }, { $pull: { connections.sessions: { device: "mobile" } } }, { new: true });

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.