denodrivers / mongo

🍃 MongoDB driver for Deno 🦕
https://deno.land/x/mongo
MIT License
514 stars 95 forks source link

MongoError: findAndModify - Either an update or remove=true must be specified #373

Closed aldaMarMu closed 2 years ago

aldaMarMu commented 2 years ago

Hello,

I am trying to use findAndModify to update one document in my centers collection but it gives me the following error:

MongoError: {"ok":0,"errmsg":"Either an update or remove=true must be specified","code":9,"codeName":"FailedToParse"}

This is the code I used:

    const newCenter = await db.collection("centers").findAndModify({
      query:{ _id: new ObjectId("62e7e66fdfa1d9392721647b") },
      update: { $set: {name: "test"}},
      new: true
    });

If I execute the same query on mongo shell or with a mongo client, it works perfectly.

Thank you!

erfanium commented 2 years ago

@aldaMarMu findAndModify is deprecated in all new drivers. we will also remove this method in future. use either one of findOneAndUpdate, findOneAndReplace or findOneAndDelete

aldaMarMu commented 2 years ago

Hi @erfanium, thank you for your answer.

If I try to use findOneAndUpdate it gives me an error too: findOneAndUpdate is not a function.

It seems that the collection object does not have that method. I have checked in https://github.com/denodrivers/mongo/blob/main/src/collection/collection.ts and I can see findAndModify but not findOneAndUpdate.

erfanium commented 2 years ago

Oh damn, we have forgot to implement these methods. for now check this

const newCenter = await db.collection("centers").findAndModify({
  _id: new ObjectId("62e7e66fdfa1d9392721647b"),
},{
  update: { $set: {name: "test"}},
  new: true
});
aldaMarMu commented 2 years ago

Hi @erfanium, thank you very much. It works!

I will use findAndModify until findOneAndUpdate is available.