
- 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
Get all the MongoDB documents but not the ones with two given criteria’s?
To get all the MongoDB documents with some given criteria’s, following any of the below given cases
Case 1 Following is the query to get all the documents without a single criterion using $ne operator
db.yourCollectionName.find({yourFieldName:{$ne:"yourValue"}}).pretty();
Case 2 Following is the query to get all the documents without two given criterions using $nin operator
db.yourCollectionName.find({yourFieldName:{$nin:["yourValue1","yourValue2"]}}).pretty();
Let us first create a collection. Following is the query to create a collection with documents
>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Larry","StudentSubjectName":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d82330fd0aa0d2fe4d2") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Chris","StudentSubjectName":"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d8f330fd0aa0d2fe4d3") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Robert","StudentSubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d99330fd0aa0d2fe4d4") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"David","StudentSubjectName":"Python"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993da4330fd0aa0d2fe4d5") }
Following is the query to display all documents from a collection with the help of find() method
> db.findAllExceptFromOneOrtwoDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c993d82330fd0aa0d2fe4d2"), "StudentName" : "Larry", "StudentSubjectName" : "Java" } { "_id" : ObjectId("5c993d8f330fd0aa0d2fe4d3"), "StudentName" : "Chris", "StudentSubjectName" : "C++" } { "_id" : ObjectId("5c993d99330fd0aa0d2fe4d4"), "StudentName" : "Robert", "StudentSubjectName" : "C" } { "_id" : ObjectId("5c993da4330fd0aa0d2fe4d5"), "StudentName" : "David", "StudentSubjectName" : "Python" }
Case 1 Single Criteria
Following is the query
> db.findAllExceptFromOneOrtwoDemo.find({StudentSubjectName:{$ne:"C"}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c993d82330fd0aa0d2fe4d2"), "StudentName" : "Larry", "StudentSubjectName" : "Java" } { "_id" : ObjectId("5c993d8f330fd0aa0d2fe4d3"), "StudentName" : "Chris", "StudentSubjectName" : "C++" } { "_id" : ObjectId("5c993da4330fd0aa0d2fe4d5"), "StudentName" : "David", "StudentSubjectName" : "Python" }
Case 2 Two criterions
Following is the query
>db.findAllExceptFromOneOrtwoDemo.find({StudentSubjectName:{$nin:["C++","Python"]}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c993d82330fd0aa0d2fe4d2"), "StudentName" : "Larry", "StudentSubjectName" : "Java" } { "_id" : ObjectId("5c993d99330fd0aa0d2fe4d4"), "StudentName" : "Robert", "StudentSubjectName" : "C" }
- Related Articles
- MongoDB aggregation to get two documents with the least marks
- Get the size of all the documents in a MongoDB query?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Fetch all the ids of MongoDB documents?
- MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java”
- Group all documents with common fields in MongoDB?
- Query MongoDB with length criteria?
- Get the last two values with MongoDB
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Get the maximum mark records from a collection with documents in MongoDB
- Update the last row with search criteria in MongoDB?
- Get the maximum mark records from a collection with documents in MongoDB query
- Match MongoDB documents with fields not containing values in array?
- How to get only files but not the folders with Get-ChildItem using PowerShell?

Advertisements