findOneAndUpdate Problem?

Hi guys!

I’m studing MongoDB with a free comunity edition on Ubuntu Linux 22.04. I tried to use findOneAndModify with some parameters like:

db.getSiblingDB(‘MYDB’).collection.findOneAndUpdate(
{ _id: ObjectId(‘xxxxxxxxxxxxxxxxxxx’) },
{ $set: { licencePrice: 2500 } },
{upsert: false},
{returnDocument: “after” },
{returnNewDocument: true}
);

And the returned document is always the ‘old’ one!

Someone can view if this is a bug or my edition/O.S/code is incorrect?

Here an image to help understanding:

Your syntax is off a bit, the options are not each in an object but combined, i.e. it should be:

db.getCollection('demo').findOneAndUpdate(
{ _id: ObjectId('66dea6c0a5cac9394b072f8a') },
{ $set: { licencePrice: 2700 } },
{
    upsert: false,
    returnDocument: 'after',
    returnNewDocument: true
},
);

as opposed to:

db.getSiblingDB(‘MYDB’).collection.findOneAndUpdate(
{ _id: ObjectId(‘xxxxxxxxxxxxxxxxxxx’) },
{ $set: { licencePrice: 2500 } },
{upsert: false},
{returnDocument: “after” },
{returnNewDocument: true}
);
2 Likes

You’re correct! Thanks very much!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.