
- 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 to properly use 'exist' function in MongoDB like in SQL?n
To check for existence of a record, use findOne() in MongoDB. Let us first create a collection with documents −
> db.existsAlternateDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d23f9e4dae213890ac5c") } > db.existsAlternateDemo.insertOne({"StudentName":"Chris","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d2559e4dae213890ac5d") } >db.existsAlternateDemo.insertOne({"StudentName":"Chris","StudentAge":22,"StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d2689e4dae213890ac5e") }
Following is the query to display all documents from a collection with the help of find() method −
> db.existsAlternateDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5e06d23f9e4dae213890ac5c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e06d2559e4dae213890ac5d"), "StudentName" : "Chris", "StudentAge" : 21 } { "_id" : ObjectId("5e06d2689e4dae213890ac5e"), "StudentName" : "Chris", "StudentAge" : 22, "StudentCountryName" : "US" }
Here is the query to properly use 'exist' function in MongoDB like in SQL i.e. findOne() −
> db.existsAlternateDemo.findOne({"StudentCountryName" : "US"});
This will produce the following output −
{ "_id" : ObjectId("5e06d2689e4dae213890ac5e"), "StudentName" : "Chris", "StudentAge" : 22, "StudentCountryName" : "US" }
- Related Articles
- How to properly use h1 in HTML5?
- What is the equivalent of SQL “like” in MongoDB?
- How to use deleteOne() function in MongoDB?
- Explain the use of sql LIKE operator using MySQL in Python?
- How can I use 'Not Like' operator in MongoDB?
- How to use my object like an array using map function in JavaScript?
- How to use parameterized SQL query in a JSP?
- How to query MongoDB with “like”?
- How to use LIKE Cause in Android sqlite?
- How to use $regex in MongoDB?
- How to use $type in MongoDB?
- How to query MongoDB similar to “like” ?
- How to use user variables in MySQL LIKE clause?
- How to use collMod in MongoDB runCommand()?
- How to use save() correctly in MongoDB?

Advertisements