
- 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
MongoDB query for ranking / search count?
For this, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo120.insertOne( ... { ... 'Name': 'Chris', ... 'Subjects': [ 'MySQL', 'MongoDB', 'Java', 'Python' ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2f11aed8f64a552dae6365") } > db.demo120.insertOne( ... { ... 'Name': 'Bob', ... 'Subjects': [ 'C', 'MongoDB' ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2f11afd8f64a552dae6366") }
Display all documents from a collection with the help of find() method −
> db.demo120.find();
This will produce the following output −
{ "_id" : ObjectId("5e2f11aed8f64a552dae6365"), "Name" : "Chris", "Subjects" : [ "MySQL", "MongoDB", "Java", "Python" ] } { "_id" : ObjectId("5e2f11afd8f64a552dae6366"), "Name" : "Bob", "Subjects" : [ "C", "MongoDB" ] }
Following is the query for MongoDB ranking / search count −
> var s = ['MySQL', 'Java', 'MongoDB']; > db.demo120.aggregate([ ... { "$match": { "Subjects": { "$in": s } } }, ... { ... "$addFields": { ... "RankSearch": { ... "$divide": [ ... { "$size": { "$setIntersection": ["$Subjects",s] } }, ... { "$size": "$Subjects" } ... ] ... } ... } ... }, ... { "$sort": { "RankSearch": -1 } } ... ])
This will produce the following output −
{ "_id" : ObjectId("5e2f11aed8f64a552dae6365"), "Name" : "Chris", "Subjects" : [ "MySQL", "MongoDB", "Java", "Python" ], "RankSearch" : 0.75 } { "_id" : ObjectId("5e2f11afd8f64a552dae6366"), "Name" : "Bob", "Subjects" : [ "C", "MongoDB" ], "RankSearch" : 0.5 }
- Related Articles
- Query MongoDB for a nested search
- MongoDB query for specific case insensitive search
- MongoDB query to search for Month and Day only?
- MongoDB query with case insensitive search?
- MongoDB Query to search for records only in a specific hour?
- How to improve the ranking of your websites for search engines
- MongoDB query to search for string like “@email” in the field values
- MongoDB query to select distinct and count?
- Search for multiple documents in MongoDB?
- MongoDB query for exact match
- MongoDB query for array concatenation?
- MySQL database field type for search query?
- MongoDB query to search date records using only Month and Day
- Query for multiple parameters in MongoDB?
- MongoDB query for a single field

Advertisements