MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Mongo $ addToSet con più valori sintassi corretta

Devi aggiungere un oggetto per impostare UsersAndQuestions:

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

AGGIORNAMENTO.

Lo farei con due domande:

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){

                })
        }
})