MongoDB查询到';返回空结果集';

MongoDB查询到';返回空结果集';,mongodb,Mongodb,我正在代码中动态构造和修改查询 我希望某个代码路径使集合返回0个文档 我只需执行Collection.find({u id:null})。这似乎奏效了 只是想知道在MongoDB中是否有更标准的方法来实现这一点。谢谢 null是_id的有效值,因此,尽管该查询可能适合您的数据,但通常不能保证生成空结果集: MongoDB Enterprise > db.foo.insert({_id:null}) WriteResult({ "nInserted" : 1 }) Mon

我正在代码中动态构造和修改查询

我希望某个代码路径使集合返回0个文档

我只需执行
Collection.find({u id:null})
。这似乎奏效了


只是想知道在MongoDB中是否有更标准的方法来实现这一点。谢谢

null是_id的有效值,因此,尽管该查询可能适合您的数据,但通常不能保证生成空结果集:

MongoDB Enterprise > db.foo.insert({_id:null})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.foo.find()
{ "_id" : null }
MongoDB Enterprise > db.foo.find({_id:null})
{ "_id" : null }
MongoDB Enterprise > 
db.foo.find({_id:{$exists:false}})
_但是,id确实需要存在于每个文档中,因此坚持它不存在的查询应始终返回空结果集:

MongoDB Enterprise > db.foo.insert({_id:null})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.foo.find()
{ "_id" : null }
MongoDB Enterprise > db.foo.find({_id:null})
{ "_id" : null }
MongoDB Enterprise > 
db.foo.find({_id:{$exists:false}})

当然,这只适用于顶级文档上的查询。

null
对于我的数据很好,因为在
\u id
上有验证。但你的解决方案是密封的。谢谢