gpt4 book ai didi

ruby - 在 Mongoid 中插入

转载 作者:IT老高 更新时间:2023-10-28 12:31:00 24 4
gpt4 key购买 nike

在 Mongoid 中是否有一种内置的方式来制作 upsert(如果不存在则插入)?或者我应该先检查一个项目是否存在,然后再进行插入/更新?

最佳答案

Mongoid 已经内置了 upsert 方法

Performs a MongoDB upsert on the document. If the document exists in the database, it will get overwritten with the current attributes of the document in memory. If the document does not exist in the database, it will be inserted. Note that this only runs the {before|after|around}_upsert callbacks.

取自 https://www.mongodb.com/docs/mongoid/7.3/tutorials/mongoid-persistence/

关于ruby - 在 Mongoid 中插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328028/

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