Get all the MongoDB documents but not the ones with two given criteria’s?

MongoDBDatabaseBig Data Analytics

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"
}
raja
Published on 11-Apr-2019 14:14:24
Advertisements