gpt4 book ai didi

ruby - Mongoid 删除文档匹配ID

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:33 25 4
gpt4 key购买 nike

我正在尝试删除我收藏中的重复文档。这是我的代码:

class Sites
include Mongoid::Document
store_in collection: "sites"
end

Sites.destroy_all(conditions: { _id: BSON::ObjectId("5685a45be4b06ab5911dcd12")})

这是返回的内容:

D, [2015-12-31T17:39:16.488657 #10126] DEBUG -- : MONGODB | localhost:27017 | rails.find | STARTED | {"find"=>"sites", "filter"=>{"conditions"=>{"_id"=>BSON::ObjectId('5685a45be4b06ab5911dcd12')}}}
D, [2015-12-31T17:39:16.488946 #10126] DEBUG -- : MONGODB | localhost:27017 | rails.find | SUCCEEDED | 0.000189083s

但是当我搜索文档时它仍然存在 :(

{"_id"=>BSON::ObjectId('5685a45be4b06ab5911dcd12'), "name"=>"StackOverflow", "title"=>"Stack Overflow", "type"=>"Forum", "url"=>"http://www.stackoverflow.com"}

如何使用 Mongoid 通过引用永久删除文档?

最佳答案

5.x 的 Mongoid 更接近于我们习惯的 AR 方法,因此您的 id 字段的行为方式类似。

Sites.where(id: '5685a45be4b06ab5911dcd12').delete

要调用所有必要的回调,只需使用 .destroy。只知道这会将所有内容加载到内存中并且可能很昂贵。

Sites.where(id: '5685a45be4b06ab5911dcd12').destroy

关于ruby - Mongoid 删除文档匹配ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552202/

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