gpt4 book ai didi

mongodb - Mongoose 中的 updateMany() 和 bulkWrite() 有什么区别?

转载 作者:行者123 更新时间:2023-12-03 13:57:29 26 4
gpt4 key购买 nike

AFAIK,这两个操作都可以更新多个文档。如果是这样,两者之间有什么区别?

最佳答案

只是引用 API docs这里

bulkWrite

Sends multiple insertOne, updateOne, updateMany, replaceOne, deleteOne, and/or deleteMany operations to the MongoDB server in one command. This is faster than sending multiple independent operations (like) if you use create()) because with bulkWrite() there is only one round trip to MongoDB.

Mongoose will perform casting on all operations you provide.

This function does not trigger any middleware, not save() nor update(). If you need to trigger save() middleware for every document use create() instead.



updateMany

Same as update(), except MongoDB will update all documents that match filter (as opposed to just the first one) regardless of the value of the multi option.

Note updateMany will not fire update middleware. Use pre('updateMany') and post('updateMany') instead.



(强调我的)

要查看使用示例,请查看函数各自文档中提供的示例。

关于mongodb - Mongoose 中的 updateMany() 和 bulkWrite() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com