Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo $addToSet with multiple values correct syntax

I have this mongoose schema:

var listingSchema = new Schema({
        street          : String,
        buildingNumber  : Number,
        apartmentNumber : Number,
        UsersAndQuestions: [{
            userID: String,
            questionID: [String]
        }]
});

And I just want to update it with a new entry to UsersAndQuestions which will consist of a userID which is a String, and a questionID which is also a String (but needs to be inserted into an array).

I am using this PUT request:

app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addUserInput/:userid/:listingid/:questionid')

So I have all the necessary parameters in hand.

Usually, when I wanted to update a field in a schema I used this code that I wrote:

app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addReportedUser/:userid/:listingid', function (req, res) {
        var listingToUpdate = req.params.listingid;
        var idToAdd = req.params.userid;
        Listing.update({_id: ObjectId(listingToUpdate)},
            {$addToSet: {reportedUsersIDs: ObjectId(idToAdd)}}
            , function (err) {
                if (err) {
                    res.send("There was a problem adding the reportedUserID to the listing" + err);
                }
                else {
                    console.log("Success adding reportedUserID to listing!");
                }
            })
    });

You can see I used $addToSet and it worked well. But now I want to add two parameters to a field which is an array. I thought about doing something like this:

app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addUserInput/:userid/:listingid/:questionid', function(req,res){
        var listingToUpdate = req.params.listingid;
        var idToAdd = req.params.userid;
        var questionToAdd = req.params.questionid;
        Listing.update({_id: ObjectId(listingToUpdate)},
            {$addToSet: {UsersAndQuestions.userID : ObjectId(idToAdd), UsersAndQuestions.questionID : ObjectId(questionToAdd)}}
        , function (err) {
                if (err) {
                    res.send("There was a problem adding the user and question to the listing" + err);
                }
                else{
                    console.log("Success adding user and question to the listing!");
                }
            })
    });

But I'm obviously getting a SyntaxError.

What is the correct syntax for doing what I tried to do?

Thanks a lot! :)

like image 374
Idos Avatar asked Apr 19 '26 13:04

Idos


1 Answers

You need to add object to set UsersAndQuestions:

{$addToSet: {UsersAndQuestions: { userID: idToAdd, questionID: questionToAdd } }}

UPDATE.

I would do it with two queries:

Listing.update({_id: ObjectId(listingToUpdate), 'UsersAndQuestions.userID': idToAdd}, 
    {"$addToSet": {"UsersAndQuestions.$.questionID": questionToAdd}}
    , function (err, result) {
        if(result.n === 0){
            //we haven't found document with the userId - idToAdd
            //we need to insert to UsersAndQuestions document with this user
            Listing.update({_id: ObjectId(listingToUpdate)},
                {$addToSet: {UsersAndQuestions: { userID: idToAdd, questionID: questionToAdd } }}, 
                function(err, res){

                })
        }
})
like image 199
Volodymyr Synytskyi Avatar answered Apr 21 '26 04:04

Volodymyr Synytskyi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!