

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 Questions & Answers
- MongoDB aggregation to get two documents with the least marks
- Get the size of all the documents in a MongoDB query?
- Fetch all the ids of MongoDB documents?
- Get the last two values with MongoDB
- Query MongoDB with length criteria?
- How to get only files but not the folders with Get-ChildItem using PowerShell?
- Update the last row with search criteria in MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Group all documents with common fields in MongoDB?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Get the maximum mark records from a collection with documents in MongoDB
- Return a new array of given shape filled with ones but with different datatype in Numpy
- Find all documents that have two specific id's in an array of objects in MongoDB?
- MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java”
- How to get only folders but not the files in the Get-ChildItem in PowerShell?
Advertisements