在数据库中查找空文档 [英] Find empty documents in a database

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

问题描述

我已经查询了一个安静不一致的API,因此不会返回所有数字索引的对象(但大多数).为了进一步处理数字索引上的.count(),我一直在使用db.collection.insert({})

I have queried an API which is quiet inconsistent and therefore does not return objects for all numerical indexes (but most of them). To further go on with .count() on the numerical index I've been inserting empty documents with db.collection.insert({})

我现在的问题是:我将如何查找和计算这些对象? 诸如db.collection.count({})之类的东西显然无法正常工作.

My question now is: how would I find and count these objects? Something like db.collection.count({}) won't work obviously.

感谢任何想法!

推荐答案

使用$ where运算符. Javascript表达式仅返回包含单个键的文档. (该单键为文档"_id"键)

Use the $where operator. The Javascript expression returns only documents containing a single key. (that single key being the documents "_id" key)

db.collection.find({ "$where": "return Object.keys(this).length == 1" }).count()

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

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