
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Find document in MongoDB where at least one item from an array is not in the other?
For this, set regex in MongoDB find(). Let us create a collection with documents −
> db.demo228.insertOne({"Subjects":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fa51f03d395bdc213473b") } > db.demo228.insertOne({"Subjects":["MongoDB","Java","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fa52c03d395bdc213473c") }
Display all documents from a collection with the help of find() method −
> db.demo228.find();
This will produce the following output −
{ "_id" : ObjectId("5e3fa51f03d395bdc213473b"), "Subjects" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5e3fa52c03d395bdc213473c"), "Subjects" : [ "MongoDB", "Java", "MySQL" ] }
Following is the query to find documents where at least one item from an array is not in the other −
> db.demo228.find({ "Subjects": /^(?!MongoDB|Java)/ });
This will produce the following output −
{ "_id" : ObjectId("5e3fa52c03d395bdc213473c"), "Subjects" : [ "MongoDB", "Java", "MySQL" ] }
- Related Articles
- Find the MongoDB document from sub array?
- Count pairs in an array such that frequency of one is at least value of other in C++
- Removing item from array in MongoDB?
- Find MongoDB records where array field is not empty?
- Print array elements that are divisible by at-least one other in C++
- Count pairs in an array such that at least one element is prime in C++
- How to update a MongoDB document for adding a new item to an array?
- Get at least one match in list querying with MongoDB?
- Find Array Elements Which has at Least One Smaller Element in Java
- How to aggregate two collections where a field from one collection is greater than the other in MongoDB?
- How to query a document in MongoDB comparing fields from an array?
- From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
- MongoDB query to remove item from array?
- How to project specific fields from a document inside an array in Mongodb?
- How do I remove a string from an array in a MongoDB document?

Advertisements