javascript - mongoose remove an object from a nested array - Stack Overflow

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

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.

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.

Share Improve this question asked Feb 6, 2018 at 9:58 BonnardBonnard 3892 gold badges9 silver badges27 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 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'
        });
    }
);
Users.findOneAndUpdate(
    { "userId": "myId",  connections: { "$elemMatch": {dateConnectedUnix: 1334567891} } },
    { $pull: { "connections.$.sessions" : { device: "mobile" } } });

$elemMatch is required

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

Users.findOneAndUpdate({ _id: "myId" }, { $pull: { connections.sessions: { device: "mobile" } } }, { new: true });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742359472a4429103.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信