
- 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
How to get tag count in MongoDB query results based on list of names?
You can use $in operator. Let us first create a collection with documents −
> db.tagCountDemo.insertOne({"ListOfNames":["John","Sam","Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b387924bb85b3f48944") } > db.tagCountDemo.insertOne({"ListOfNames":["Bob","David","John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b4b7924bb85b3f48945") } > db.tagCountDemo.insertOne({"ListOfNames":["Mike","Robert","Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b5d7924bb85b3f48946") } > db.tagCountDemo.insertOne({"ListOfNames":["James","Carol","Jace"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b717924bb85b3f48947") }
Following is the query to display all documents from a collection with the help of find() method −
> db.tagCountDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : [ "John", "Sam", "Carol" ] } { "_id" : ObjectId("5cd64b4b7924bb85b3f48945"), "ListOfNames" : [ "Bob", "David", "John" ] } { "_id" : ObjectId("5cd64b5d7924bb85b3f48946"), "ListOfNames" : [ "Mike", "Robert", "Chris" ] } { "_id" : ObjectId("5cd64b717924bb85b3f48947"), "ListOfNames" : [ "James", "Carol", "Jace" ] }
Following is the query to get tag count in MongoDB
> db.tagCountDemo.find({"ListOfNames":{$in:['John','Carol']}});
This will produce the following output −
{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : [ "John", "Sam", "Carol" ] } { "_id" : ObjectId("5cd64b4b7924bb85b3f48945"), "ListOfNames" : [ "Bob", "David", "John" ] } { "_id" : ObjectId("5cd64b717924bb85b3f48947"), "ListOfNames" : [ "James", "Carol", "Jace" ] }
- Related Articles
- How to query on list field in MongoDB?
- Return query based on date in MongoDB?
- MongoDB query to update tag
- MongoDB query to get specific list of names from documents where the value of a field is an array
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- MongoDB Group query to get the count of repeated marks in documents?
- How to get row index or column index based on their names in R?
- How to get a rating average in MongoDB based on duplicate ids?
- MongoDB query to count records on the basis of matching criteria
- MongoDB query to fetch only the “Name” field based on roles?
- Push query results into variable with MongoDB?
- How to subset a named vector based on names in R?
- How to add named vectors of different sizes based on names in R?
- How to concatenate results in MongoDB?
- How to get a list of parameter names inside Python function?

Advertisements