gpt4 book ai didi

MongoDB 删除不在 fs.files 中的 fs.chunks

转载 作者:可可西里 更新时间:2023-11-01 09:07:39 31 4
gpt4 key购买 nike

我在 fs.chunks 中有 10 GB 的数据,我想删除不在 fs.files 上的所有文档。我已经删除了我不想要的 fs.files 中的每个条目,所以 fs.files 中的每个 id 都是我想保留的文件。

所以,我想要类似 db.fs.chunks.remove({"_id": {$nin: fs.files._id}}) 或“删除 fs.chunks 中的每个条目fs.files 中不存在。

编辑:我正在寻找 SQL 的 mongo 等价物 delete from fs_chunks where id not in (select id from fs_files)

最佳答案

除了执行查找然后使用 forEach 迭代之外,我不认为有一种简单的方法可以做到这一点。所以像这样:

function removeChunkIfNoOwner(chunk){
//Look for the parent file
var parentCount = db.fs.files.find({'_id' : chunk.files_id}).count();

if (parentCount === 0 ){
db.fs.chunks.remove({'_id': chunk._id});
print("Removing chunk " + chunk._id);
}
}

db.fs.chunks.find().forEach(removeChunkIfNoOwner);

如果您创建这样的函数,您会发现这种方法应该有效:

function listParentFile(chunk){
var parent = db.fs.files.findOne({'_id' : chunk.files_id});
printjson(parent);
}
db.fs.chunks.find().forEach(listParentFile);

关于MongoDB 删除不在 fs.files 中的 fs.chunks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9737953/

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