- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find datatype of all the fields in MongoDB?
Use typeof to find datatype of all the fields −
typeof db.yourCollectionName.findOne().yourFieldName;
Let us first create a collection with documents −
> db.findDataTypeDemo.insertOne({"ClientName":"Chris","isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5ccf2064dceb9a92e6aa1952") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findDataTypeDemo.findOne();
This will produce the following output −
{ "_id" : ObjectId("5ccf2064dceb9a92e6aa1952"), "ClientName" : "Chris", "isMarried" : false }
Following is the query to find datatype of a field in MongoDB −
> typeof db.findDataTypeDemo.findOne().isMarried;
This will produce the following output −
Boolean
Here is the query to get the data type of another field −
> typeof db.findDataTypeDemo.findOne().ClientName;
This will produce the following output −
String
You can get the value also. The query is as follows −
> db.findDataTypeDemo.findOne().ClientName; Chris > db.findDataTypeDemo.findOne().isMarried; False
- Related Articles
- How to retrieve all nested fields from MongoDB collection?
- Include all existing fields and add new fields to document in MongoDB?
- How do I work with array fields in MongoDB to match all?
- MongoDB query to display all the fields value, except _id
- The collection.find() always returns all fields with MongoDB?
- Get all fields names in a MongoDB collection?
- Group all documents with common fields in MongoDB?
- Select special fields rather than all in MongoDB
- Find sum of fields inside array in MongoDB?
- Sort array in MongoDB query and project all fields?
- How to remove key fields in MongoDB?
- What is the fastest way to update the whole document (all fields) in MongoDB?
- How to re-map the fields of a MongoDB collection?
- MongoDB collection query to exclude some fields in find()?
- Filter documents in MongoDB if all keys exist as fields?

Advertisements