
- 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
Search for documents with similar arrays in MongoDB and order by similarity value
Let us create a collection with documents −
> db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'Java']}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f24ac140daf4c2a3544b8") } > db.demo123.insertOne({"ListOfSubject":['Python', 'MongoDB', 'C']}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f24cd140daf4c2a3544b9") } > db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'C++']}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f24ce140daf4c2a3544ba") }
Display all documents from a collection with the help of find() method −
> db.demo123.find();
This will produce the following output −
{ "_id" : ObjectId("5e2f24ac140daf4c2a3544b8"), "ListOfSubject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e2f24cd140daf4c2a3544b9"), "ListOfSubject" : [ "Python", "MongoDB", "C" ] } { "_id" : ObjectId("5e2f24ce140daf4c2a3544ba"), "ListOfSubject" : [ "MySQL", "MongoDB", "C++" ] }
Following is the query to search for document with similar arrays and order them −
> var subjects = ['MySQL', 'MongoDB', 'Java']; > db.demo123.aggregate([ ... {$unwind: "$ListOfSubject"}, ... {$match: {ListOfSubject:{ $in:subjects}}}, ... {$group: {_id: "$_id", number: {$sum: 1}}}, ... {$project: {_id: 1, number: 1, percentage: {$divide: ["$number",subjects.length]}}}, ... {$sort: {percentage: -1}} ... ]);
This will produce the following output −
{ "_id" : ObjectId("5e2f24ac140daf4c2a3544b8"), "number" : 3, "percentage" : 1 } { "_id" : ObjectId("5e2f24ce140daf4c2a3544ba"), "number" : 2, "percentage" : 0.6666666666666666 } { "_id" : ObjectId("5e2f24cd140daf4c2a3544b9"), "number" : 1, "percentage" : 0.3333333333333333 }
- Related Articles
- Search for multiple documents in MongoDB?
- Search for documents matching first item in an array with MongoDB?
- Finding highest value from sub-arrays in MongoDB documents?
- MongoDB aggregation of elements with similar ids in different documents?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- How to search for documents based on the value of adding two properties in MongoDB?
- Sort the MongoDB documents in ascending order with aggregation?
- Sort MongoDB documents in descending order
- MongoDB query for documents matching array, irrespective of elements order
- Search by property name for any document with that property in MongoDB?
- Check for existing documents/embedded documents in MongoDB
- MongoDB aggregation to fetch documents with specific field value?
- Retrieving group by result with arrays in MongoDB?
- How to search documents through an integer array in MongoDB?
- Find a value in lowercase from a MongoDB collection with documents

Advertisements