要在MongoDB中查找字段为NAN的所有文档,请使用以下语法
db.yourCollectionName.find( { yourFieldName: NaN })
首先让我们创建一个包含文档的集合
> db.nanDemo.insertOne({"Score":0/0}); { "acknowledged" : true, "insertedId" : ObjectId("5ca251a26304881c5ce84b8a") } > db.nanDemo.insertOne({"Score":10/5}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2520e6304881c5ce84b8b") } > db.nanDemo.insertOne({"Score":20/0}); { "acknowledged" : true, "insertedId" : ObjectId("5ca252156304881c5ce84b8c") } > db.nanDemo.insertOne({"Score":0/20}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2521e6304881c5ce84b8d") }
以下是在find()
方法的帮助下显示集合中所有文档的查询
> db.nanDemo.find().pretty();
这将产生以下输出
{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" : NaN } { "_id" : ObjectId("5ca2520e6304881c5ce84b8b"), "Score" : 2 } { "_id" : ObjectId("5ca252156304881c5ce84b8c"), "Score" : Infinity } { "_id" : ObjectId("5ca2521e6304881c5ce84b8d"), "Score" : 0 }
以下是在MongoDB中查找字段为NAN的所有文档的查询
> db.nanDemo.find( { Score: 0/0 });
这将产生以下输出
{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" : NaN }
备用查询如下
> db.nanDemo.find( { Score: NaN })
这将产生以下输出
{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" : NaN }