- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 use $type in MongoDB?
The $type selects documents where the value of the field is an instance of the specified BSON type. Let us create a collection with documents −
> db.demo615.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"100"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"300"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":300}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo615.find();
This will produce the following output −
{ "_id" : ObjectId("5e99bb3465492f6c60d0027f"), "Value" : 100 } { "_id" : ObjectId("5e99bb3865492f6c60d00280"), "Value" : "100" } { "_id" : ObjectId("5e99bb3c65492f6c60d00281"), "Value" : "300" } { "_id" : ObjectId("5e99bb4265492f6c60d00282"), "Value" : 300 }
Following is the query to use MongoDB find without a hexadecimal _id −
> db.demo615.find({ "Value" : { $type : "string"} } );
This will produce the following output −
{ "_id" : ObjectId("5e99bb3865492f6c60d00280"), "Value" : "100" } { "_id" : ObjectId("5e99bb3c65492f6c60d00281"), "Value" : "300" }
Advertisements