如何在mongodb中查找空文档? [英] How to find null documents in mongodb?

查看:56
本文介绍了如何在mongodb中查找空文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的 mongodb 初学者.实际上,我正在尝试查找所有包含 null 或不包含任何内容的文档,例如 {_id":ABC"} 用于从集合中删除它们.

I'm a complete beginner in mongodb . Actually I'm trying to find all the documents containing null or nothing for example documents like { "_id" : "abc" } for deleting them from collection.

但即使在搜索了很多 SO 问题后,我也找不到任何解决方案.那么,我该怎么做?对不起,如果我忽略了什么.

But even after searching a lot of SO questions I couldn't get any solution .So, how can I do this ? and sorry if I'm ignoring anything.

推荐答案

我不知道如何在单个操作中完成,但您可以尝试这样的操作:

I don't know how to do it in a single operation, but you can try something like this:

db["collectionName"].find({_id: {$exists: true}}).forEach(function(doc) {
  if (Object.keys(doc).length === 1) {
    // ..delete this document db["collectionName"].remove({_id: doc._id})
  }
})

这篇关于如何在mongodb中查找空文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆