在MongoDB中,有以下几种方法可以修改数据:
updateOne():用于更新集合中满足指定条件的第一条文档。如果没有满足条件的文档,则不进行任何操作。db.collection.updateOne( <filter>, <update>, { upsert: <boolean>, writeConcern: <document> })updateMany():用于更新集合中满足指定条件的所有文档。db.collection.updateMany( <filter>, <update>, { upsert: <boolean>, writeConcern: <document> })replaceOne():用新文档替换集合中满足指定条件的第一条文档。如果没有满足条件的文档,则不进行任何操作。db.collection.replaceOne( <filter>, <replacement>, { upsert: <boolean>, writeConcern: <document> })findOneAndUpdate():用于返回并更新集合中满足指定条件的第一条文档。db.collection.findOneAndUpdate( <filter>, <update>, { projection: <document>, sort: <document>, maxTimeMS: <number>, upsert: <boolean>, returnNewDocument: <boolean>, collation: <document>, arrayFilters: [ <filterdocument1>, ... ], hint: <document|string> })findOneAndReplace():用新文档替换集合中满足指定条件的第一条文档,并返回替换前的文档。db.collection.findOneAndReplace( <filter>, <replacement>, { projection: <document>, sort: <document>, maxTimeMS: <number>, upsert: <boolean>, returnNewDocument: <boolean>, collation: <document>, hint: <document|string> })findOneAndDelete():删除并返回集合中满足指定条件的第一条文档。db.collection.findOneAndDelete( <filter>, { projection: <document>, sort: <document>, maxTimeMS: <number>, collation: <document>, hint: <document|string> })上述方法中,<filter>参数用于指定筛选条件,<update>参数用于指定要进行的修改操作,<replacement>参数用于指定要替换的文档。

