Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB删除不在fs.files中的fs.chunk_Mongodb - Fatal编程技术网

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

MongoDB删除不在fs.files中的fs.chunk,mongodb,Mongodb,我在fs.chunk中有10 GB的数据,我想删除所有不在fs.files上的文档。我已经删除了fs.files中我不想要的每个条目,因此fs.files中的每个id都是我想要保留的文件 因此,我需要类似db.fs.chunks的东西。删除{u id:{$nin:fs.files.{u id}}或删除fs.chunks中不存在于fs.files中的所有条目 编辑: 我正在寻找mongo等效的SQL从fs_块中删除,其中id不在fs_文件的select id中。我认为除了执行查找然后使用forEa

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

因此,我需要类似db.fs.chunks的东西。删除{u id:{$nin:fs.files.{u id}}或删除fs.chunks中不存在于fs.files中的所有条目

编辑:
我正在寻找mongo等效的SQL从fs_块中删除,其中id不在fs_文件的select id中。

我认为除了执行查找然后使用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);

我发现这个基于Mick的解决方案可以更快地处理大量块:

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

  if (parentCount === 0 ){
      res = db.fs.chunks.remove({'files_id':files_id})
  }
}

files = db.fs.chunks.distinct('files_id').forEach(removeChunkIfNoOwner)

使用distinct对我不起作用,因为我的收藏太多了。对Bas的查询稍加修改对我来说很有效

function removeChunkIfNoOwner(chunk){
  var parentCount = db.fs.files.find({'_id' : chunk.files_id}).count();

  if(parentCount === 0){
    res = db.fs.chunks.remove({'files_id':chunk._id})
    print("item removed")
  }
}
files = db.fs.chunks.find({files_id:{$exists:1}},{data:0}).forEach(removeChunkIfNoOwner)


作为记录,我想知道您是否可以这样做:db.collection.find{'fields.Tags.value.uuid':{$in:db.collection.find{otherId:'8FAAD502-9F8D-4F84-80DC-D9C8A5801737'},{uuid:1,{u id:0}.toArray}。。。但它看起来不起作用!如果您有很多文件,您应该删除打印行,这将大大加快处理速度。对于那些想知道如何使用这些代码片段的人,只需在mongo shell中使用它们。调用“mongo”然后“show dbs”列出您的数据库,选择要与“use name_of_your_db”一起使用的数据库,然后将函数代码复制/粘贴到您的CLI中。如果您的函数有输入错误。正确的是:chunks。删除{u id':chunk.\u id}而不是chunks。删除{u id':chunk.\u id}。stackoverflow编辑队列已满,因此我无法建议进行此编辑。