
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Datatype of All 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
Advertisements