Mongoid:update_attributes vs update_all

Mongoid: update_attributes vs update_all

docs 中,他们没有提到 Criteria#update_attributesCriteria#update_all 之间的细微差别。后者不更新 updated_at 文档字段。

这是期望的行为吗?如果是这样,它不应该反映在文档中吗?

隐含的假设是您熟悉 ActiveRecord,他们假设您假设 Mongoid 的行为类似于 ActiveRecord(除非它不能)。在 ActiveRecord 中,update_all is explicitly documented 不调用回调:

update_all(updates)

[...] This method constructs a single SQL UPDATE statement and sends it straight to the database. It does not instantiate the involved models and it does not trigger Active Record callbacks or validations. [...]

Mongoid 的 update_all 类似地将单个 update 操作发送到 MongoDB 而无需调用回调。

Mongoid 文档(与 Ruby 中的往常一样)在重要细节上略显单薄,因此您必须习惯于猜测和阅读您正在使用的库的源代码。包括测试来验证您的猜测也是一个好主意,可以避免升级过程中的不愉快。