1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.