

- 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 query for documents whose array elements does not have a specific value
For such cases, use $elemMatch. This operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
Let us create a collection with documents −
> db.demo239.insertOne( ... { ... "Name" : "Chris", ... "details" : [ ... { "DueDate" : new ISODate("2019-01-21"), "ProductPrice" : 1270 }, ... { "DueDate" : new ISODate("2020-02-12"), "ProductPrice" : 2000 } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e441c6bf4cebbeaebec5157") } > db.demo239.insertOne( ... { ... "Name" : "David", ... "details" : [ ... { "DueDate" : new ISODate("2018-11-11"), "ProductPrice" : 1450}, ... { "DueDate" : new ISODate("2020-02-12") } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e441c6cf4cebbeaebec5158") }
Display all documents from a collection with the help of find() method −
> db.demo239.find();
This will produce the following output −
{ "_id" : ObjectId("5e441c6bf4cebbeaebec5157"), "Name" : "Chris", "details" : [ { "DueDate" : ISODate("2019-01-21T00:00:00Z"), "ProductPrice" : 1270 }, { "DueDate" : ISODate("2020-02-12T00:00:00Z"), "ProductPrice" : 2000 } ] } { "_id" : ObjectId("5e441c6cf4cebbeaebec5158"), "Name" : "David", "details" : [ { "DueDate" : ISODate("2018-11-11T00:00:00Z"), "ProductPrice" : 1450 }, { "DueDate" : ISODate("2020-02-12T00:00:00Z") } ] }
Following is the query to fetch documents whose array elements does not have a specific value −
> db.demo239.find({ "details": { "$elemMatch": { "DueDate": { "$exists": true }, "ProductPrice": { "$exists": false } } } })
This will produce the following output −
{ "_id" : ObjectId("5e441c6cf4cebbeaebec5158"), "Name" : "David", "details" : [ { "DueDate" : ISODate("2018-11-11T00:00:00Z"), "ProductPrice" : 1450 }, { "DueDate" : ISODate("2020-02-12T00:00:00Z") } ] }
- Related Questions & Answers
- Find MongoDB documents where elements of an array have a specific value?
- Find documents where all elements of an array have a specific value in MongoDB?
- MongoDB query to determine if a specific value does not exist?
- Find MongoDB documents where all objects in array have specific value?
- MongoDB query for documents matching array, irrespective of elements order
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to find documents whose array contains a string that is a substring of a specific word
- Find value above a specific value in MongoDB documents?
- MongoDB query to fetch a document that does not have a particular field?
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB query to get only specific fields in nested array documents?
- Query an array in MongoDB to fetch a specific value
- MongoDB query to update all documents matching specific IDs
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to implement nor query to fetch documents except a specific document
Advertisements