
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How do I find all documents with a field that is NaN in MongoDB?
To find all documents with a field that is NAN in MongoDB, use the following syntax
db.yourCollectionName.find( { yourFieldName: NaN })
Let us first create a collection with documents
> 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") }
Following is the query to display all documents from a collection with the help of find() method
> db.nanDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" : NaN } { "_id" : ObjectId("5ca2520e6304881c5ce84b8b"), "Score" : 2 } { "_id" : ObjectId("5ca252156304881c5ce84b8c"), "Score" : Infinity } { "_id" : ObjectId("5ca2521e6304881c5ce84b8d"), "Score" : 0 }
Following is the query to find all documents with a field that is NAN in MongoDB
> db.nanDemo.find( { Score: 0/0 });
This will produce the following output
{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" : NaN }
The alternate query is as follows
> db.nanDemo.find( { Score: NaN })
This will produce the following output
{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" : NaN }
- Related Articles
- How can I rename a field for all documents in MongoDB?
- How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
- Find documents that contains specific field in MongoDB?
- Find MongoDB documents that contains specific field?
- Find all duplicate documents in a MongoDB collection by a key field?
- How to add a new field to all the documents in a MongoDB collection
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Find all collections in MongoDB with specific field?
- Find items that do not have a certain field in MongoDB?
- How can I sort documents in MongoDB 4 and display only a single field?
- Display only a single field from all the documents in a MongoDB collection
- How do I add a field to existing record in MongoDB?
- Group all documents with common fields in MongoDB?
- Sum MongoDB Sub-documents field?
- How to find MongoDB documents with a specific string?

Advertisements