
- 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
MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java”
For such kind of fetching, use only $nin and $in. Let us create a collection with documents −
> db.demo140.insertOne({"Id":101,"Subjects":["MongoDB","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c149fdf09dd6d08539a9") } > db.demo140.insertOne({"Id":102,"Subjects":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c14cfdf09dd6d08539aa") } > db.demo140.insertOne({"Id":103,"Subjects":["MongoDB","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c157fdf09dd6d08539ab") } > db.demo140.insertOne({"Id":104,"Subjects":["MongoDB","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c163fdf09dd6d08539ac") } > db.demo140.insertOne({"Id":105,"Subjects":["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c16ffdf09dd6d08539ad") }
Display all documents from a collection with the help of find() method −
> db.demo140.find();
This will produce the following output −
{ "_id" : ObjectId("5e31c149fdf09dd6d08539a9"), "Id" : 101, "Subjects" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5e31c14cfdf09dd6d08539aa"), "Id" : 102, "Subjects" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5e31c157fdf09dd6d08539ab"), "Id" : 103, "Subjects" : [ "MongoDB", "PL/SQL" ] } { "_id" : ObjectId("5e31c163fdf09dd6d08539ac"), "Id" : 104, "Subjects" : [ "MongoDB", "SQL Server" ] } { "_id" : ObjectId("5e31c16ffdf09dd6d08539ad"), "Id" : 105, "Subjects" : [ "C", "C++" ] }
Here is the query to fetch documents having subjects “MongoDB” but not “Java” −
> db.demo140.find({$and:[{Subjects:{$in:['MongoDB']}}, {Subjects:{$nin:['Java']}}]}).toArray();
This will produce the following output −
[ { "_id" : ObjectId("5e31c149fdf09dd6d08539a9"), "Id" : 101, "Subjects" : [ "MongoDB", "MySQL" ] }, { "_id" : ObjectId("5e31c157fdf09dd6d08539ab"), "Id" : 103, "Subjects" : [ "MongoDB", "PL/SQL" ] }, { "_id" : ObjectId("5e31c163fdf09dd6d08539ac"), "Id" : 104, "Subjects" : [ "MongoDB", "SQL Server" ] } ]
- Related Questions & Answers
- canvas.style.display = “block” not working in HTML5
- MongoDB Query for boolean field as “not true”
- “Toggle” query in MongoDB?
- MongoDB “$and” operator for subcollection to fetch a document?
- MongoDB regular expression to fetch record with specific name “John”, instead of “john”
- MongoDB indexes not working when executing $elemMatch?
- How to work with “!=” or “not equals” in MySQL WHERE?
- Using “not equal” in MySQL?
- To “user-scalable=no” or not to “user-scalable=no” in HTML5
- “Where” clause not working while updating database record in ABAP
- How to query MongoDB with “like”?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- How to query MongoDB similar to “like” ?
- MongoDB query to fetch only the “Name” field based on roles?
- Header files “stdio.h” and “stdlib.h” in C
Advertisements