c# MongoDB driver: FindOneAndUpdate how to know if it has found a document?

前端 未结 2 894

So I\'m using the MongoDB driver to update an object field value in the database if the object exist.

 IMongoDatabase db = _mongoClient.GetDatabase(DataBase);
 I         


        
2条回答
  •  轮回少年
    2021-01-26 20:53

    The mongoose documentation states in the Options section:

    Options:

    • ...
    • upsert: bool - creates the object if it doesn't exist. defaults to false.
    • ...

    There should an upsert parameter, which, if set to true, will create a new object if none was found. However, this defaults to false, so your call should not create a new database entry.

    I would assume that the C# driver should behave identically. If not, you could set the Upsert parameter to false, see here

提交回复
热议问题