查找字段数据类型不是 NaN 的所有记录

Oz1*_*123 1 javascript mongodb mongodb-query

我想找到与这个问题完全相反的问题,如何在 MongoDB 中找到具有 NaN 字段的所有文档?.

关键字$not需要一个文档,我用type.成功了,猜测 NaN 将是一个双重:

db.collection.find({'key2': {$not : {$type :1}}})
Run Code Online (Sandbox Code Playgroud)

我设法找到的另一种方法是使用$nin

db.collection.find({'key1': NaN,  'key2' : { $nin : [NaN]}}})) 
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?你能指出来吗?

Joh*_*yHK 5

您可以使用$ne运算符查找字段不包含特定值的文档。在 shell 中使用$newithNaN工作正常:

db.collection.find({key1: {$ne: NaN}})
Run Code Online (Sandbox Code Playgroud)