

- 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 - How can I see if all elements of a field are contained in a superset?
For all elements of a field in MongoDB, use find() and in that, use $elemMatch. The $elemMatch 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.demo624.insertOne({"ListOfName":["John","Chris","David","Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab3ff6c954c74be91e6a5") } > db.demo624.insertOne({"ListOfName":["John","Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab4026c954c74be91e6a6") } > db.demo624.insertOne({"ListOfName":["John","Chris","Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab4076c954c74be91e6a7") } > db.demo624.insertOne({"ListOfName":["John","Chris","Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab40e6c954c74be91e6a8") } > db.demo624.insertOne({"ListOfName":["John","Chris","Mike","Robert"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab4186c954c74be91e6a9") }
Display all documents from a collection with the help of find() method −
> db.demo624.find();
This will produce the following output −
{ "_id" : ObjectId("5e9ab3ff6c954c74be91e6a5"), "ListOfName" : [ "John", "Chris", "David", "Bob" ] } { "_id" : ObjectId("5e9ab4026c954c74be91e6a6"), "ListOfName" : [ "John", "Chris" ] } { "_id" : ObjectId("5e9ab4076c954c74be91e6a7"), "ListOfName" : [ "John", "Chris", "Carol" ] } { "_id" : ObjectId("5e9ab40e6c954c74be91e6a8"), "ListOfName" : [ "John", "Chris", "Bob" ] } { "_id" : ObjectId("5e9ab4186c954c74be91e6a9"), "ListOfName" : [ "John", "Chris", "Mike", "Robert" ] }
Following is the query to see if all elements of a field are contained in a superset −
> db.demo624.find({"ListOfName":{$not:{$elemMatch:{$nin:["John", "Chris", "David", "Bob"]}}}});
This will produce the following output −
{ "_id" : ObjectId("5e9ab3ff6c954c74be91e6a5"), "ListOfName" : [ "John", "Chris", "David", "Bob" ] } { "_id" : ObjectId("5e9ab4026c954c74be91e6a6"), "ListOfName" : [ "John", "Chris" ] } { "_id" : ObjectId("5e9ab40e6c954c74be91e6a8"), "ListOfName" : [ "John", "Chris", "Bob" ] }
- Related Questions & Answers
- How can I rename a field for all documents in MongoDB?
- How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
- How can I see the description of a MySQL Temporary Tables?
- How can I check whether a field exists or not in MongoDB?
- How can I change the field name in MongoDB?
- How can I remove all child elements of a DOM node in JavaScript?
- List all values of a certain field in MongoDB?
- How do I see all foreign keys to a table column?
- How do I find all documents with a field that is NaN in MongoDB?
- Delete all elements in an array field in MongoDB?
- Check if a field contains a string in MongoDB?
- How can I sort documents in MongoDB 4 and display only a single field?
- How can I check if a string is all uppercase in JavaScript?
- Python - Check if all elements in a List are same
- Python - Check if all elements in a list are identical
Advertisements