
- 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 dynamically build MongoDB query?
To build query dynamically, you need to write some script. Let us first create a collection with documents −
> db.dynamicQueryDemo.insertOne({"Name":"John","Subject":["MongoDB","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef5c5def71edecf6a1f69a") } > db.dynamicQueryDemo.insertOne({"Name":"John","Subject":["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef5c73ef71edecf6a1f69b") } > db.dynamicQueryDemo.insertOne({"Name":"John","Subject":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef5c8bef71edecf6a1f69c") }
Display all documents from a collection with the help of find() method −
> db.dynamicQueryDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cef5c5def71edecf6a1f69a"), "Name" : "John", "Subject" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5cef5c73ef71edecf6a1f69b"), "Name" : "John", "Subject" : [ "C", "C++" ] } { "_id" : ObjectId("5cef5c8bef71edecf6a1f69c"), "Name" : "John", "Subject" : [ "MongoDB", "Java" ] }
Following is the query to dynamically build MongoDB query −
> function findDocument(subject) { var find = {}; if (subject.length == 0) find["$nin"] = subject; else find["$in"] = subject; return find; } > var sub = ["MySQL","MongoDB"]; > var myDoc = findDocument(sub); > db.dynamicQueryDemo.aggregate([{ $match: { "Subject": myDoc, } }]);
This will produce the following output −
{ "_id" : ObjectId("5cef5c5def71edecf6a1f69a"), "Name" : "John", "Subject" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5cef5c8bef71edecf6a1f69c"), "Name" : "John", "Subject" : [ "MongoDB", "Java" ] }
- Related Articles
- MySQL query to create table dynamically?
- Build (escape) regexp in MongoDB?
- How to implement MongoDB $or query?
- How to query MongoDB with “like”?
- How to query MongoDB similar to “like” ?
- How to query MongoDB with a LIMIT?
- How to query all items in MongoDB?
- Build index in the background with MongoDB
- How to query MongoDB using the $ne operator?
- How to query on list field in MongoDB?
- How to query with nand operator in MongoDB?
- MongoDB: How to query a collection named “version”?
- How to select specific columns in MongoDB query?
- How to work with variables in MongoDB query
- MongoDB query to skip documents

Advertisements