如何在MongoDB中查找包含NaN字段的所有文档?
要查找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 }
广告