1

I'm creating and assigning a new object id to a nested array item in Mongo, which is working great. I can't figure out how to check against that object ID to verify it doesn't already exist.

Here is the route:

router.post('/events', function(req,res){
    var loggedInUser = req.user.username;
    var objectId = new ObjectID();

    User.update(
        {"username" : loggedInUser},
        {
            $push:{
                primaryEvents:{
                    _id: objectId,
                    name: req.body.name,
                    date: req.body.date,
                    description: req.body.description
                }
            }
        },
        {upsert:true},
        function(err){
            console.log('made it to the calback');
            if(err)
                res.send(err);
            res.render('index', {
                user: req.user,
                primaryEvents: req.user.primaryEvents
            });
        }
    );
});

1 Answer 1

1

You want to check that there is no value in the array with the $ne inequality operator:

        User.update(
            { 
                "username" : loggedInUser,
                "primaryEvents._id": { "$ne": objectid }
            },
            {
            $push:{
                primaryEvents:{
                    _id: objectId,
                    name: req.body.name,
                    date: req.body.date,
                    description: req.body.description
                }
            }
            },
            {upsert:true},
            function(err){
                console.log('made it to the calback');
                if(err)
                    res.send(err);
                res.render('index', { 
                    user: req.user,
                    primaryEvents: req.user.primaryEvents
                });
            }
        );

That will check all named properties of the sub-documents in the array and make sure that value does not exist.

So the "query" portion is where you control that. If there is already an _id with your value then there is no match and therefore no update.

Otherwise work off of something else you consider unique if you do not know the _id value, like "name":

        User.update(
            { 
                "username" : loggedInUser,
                "primaryEvents.name": { "$ne": req.body.name }
            },
            {
            $push:{
                primaryEvents:{
                    _id: objectId,
                    name: req.body.name,
                    date: req.body.date,
                    description: req.body.description
                }
            }
            },
            {upsert:true},
            function(err){
                console.log('made it to the calback');
                if(err)
                    res.send(err);
                res.render('index', { 
                    user: req.user,
                    primaryEvents: req.user.primaryEvents
                });
            }
        );
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, that's exactly what I needed. I think I asked the question wrong but your second solution is actually what I needed. Thanks.

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.