yss
yss

Reputation: 23

Mongodb query not working in nodejs. Why is this code not working?

My variable id has the object id in it, but when I run this updateOne query it doesn't update the document in the database. When I replace the updateOne argument with updateOne({}) it does properly update the first record in the database, so I know my set syntax is right... so the problem must be in this line of code:

{"_id": "ObjectId(\""+id+"\")"}},

but when I print that line of code out with a console.log, it looks identical to the mongoDB query which works. Can anyone help me figure out why that line doesn't work? Is there a type casting problem going on or something like that?

             db.collection('profile').updateOne(
                {"_id": "ObjectId(\""+id+"\")"}},
                { $set:
                    {
                        "star_rating": updatedProfile.test ,
                        "address.street": updatedProfile.street,
                        "address.city": updatedProfile.city,
                        "address.postalcode": updatedProfile.postal,
                        "address.country": updatedProfile.country,
                        "phonenumber": updatedProfile.phone,
                        "birthday": updatedProfile.datepicker
                     }
                }, //set
                {
                upsert: false //do not create a doc if the id doesn't exist
            }
            ); //updateOne

Upvotes: 1

Views: 133

Answers (1)

Tim
Tim

Reputation: 2715

Can you please try this and see if it works:

var ObjectID = require('mongodb').ObjectID,

db.collection('profile').updateOne(
    {"_id": new ObjectID(id)}},
    { $set:
        {
            "star_rating": updatedProfile.test ,
            "address.street": updatedProfile.street,
            "address.city": updatedProfile.city,
            "address.postalcode": updatedProfile.postal,
            "address.country": updatedProfile.country,
            "phonenumber": updatedProfile.phone,
            "birthday": updatedProfile.datepicker
         }
    }, //set
    {
    upsert: false //do not create a doc if the id doesn't exist
}); //updateOne

Upvotes: 1

Related Questions