Mongoose Query() Function
Last Updated :
10 Sep, 2020
The Query constructor is used for building the queries. So the user does not need to instantiate a Query directly. The `query` is an instance of `Query`.
Syntax:
Query()
Parameters: This function has optional parameters like model, condition, collection and options.
Return Value: This function returns Query object.
Installation of mongoose module:
npm install mongoose
- After installing the mongoose module, you can check your mongoose version in command prompt using the command.
npm version mongoose
- After that, you can just create a folder and add a file for example, index.js as shown below.
Example 1: Filename: index.js
javascript
const express = require('express');
const mongoose = require('mongoose');
const app = express()
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
const query = new mongoose.Query();
console.log(query);
app.listen(3000, function(error ){
if(error) console.log(error)
console.log("Server listening on PORT 3000")
})
Steps to run the program:
- The project structure will look like this:

- Run index.js file using below command:
node index.js
Output:
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map {}, _posts: Map {} },
_executionCount: 0,
op: undefined,
options: {},
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: undefined,
_traceFunction: undefined,
'$useProjection': true
}
Server listening on PORT 3000
Example 2: Filename: index.js
javascript
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
const query = new mongoose.Query();
query.collection(User.collection);
console.log(query);
Steps to run the program:
- The project structure will look like this:

- Run index.js file using below command:
node index.js
Output:
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map {}, _posts: Map {} },
_executionCount: 0,
op: undefined,
options: {},
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: NodeCollection {
collection: NativeCollection {
collection: null,
Promise: [Function: Promise],
_closed: false,
opts: [Object],
name: 'users',
collectionName: 'users',
conn: [NativeConnection],
queue: [],
buffer: true,
emitter: [EventEmitter]
},
collectionName: 'users'
},
_traceFunction: undefined,
'$useProjection': true
}
Reference: https://mongoosejs.com/docs/api/query.html
Similar Reads
Mongoose | where() Function The where() function is used to create a Query, applies the passed conditions and returns the Query. Installation of mongoose module:You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongooseAfter installing mongoose module, you can ch
2 min read
Mongoose Query() API The Mongoose API Query() method of the Mongoose API is used as a constructor to create a reference of Query. It allows us to create query objects and build query expressions. We do not need to create an object of the Query class in order to interact with the MongoDB collection. We can directly call
3 min read
Mongoose Query API Mongoose is a tool that allows you to define and work with documents in your MongoDB collections. One of the most important aspects of Mongoose is its Query API, which provides a set of methods for building and executing queries in your MongoDB database. In this article, we will look at some of the
5 min read
Mongoose count() Function When building applications using MongoDB with Mongoose, one common requirement is to count the number of documents in a collection. Whether you want to know how many users exist, how many orders have been placed, or simply verify how many documents match certain criteria.Mongooseâs count() function
4 min read
Mongoose.model() Function The mongoose.model() function is a core method in the Mongoose library, used for interacting with MongoDB in a Node.js environment. It helps in creating a MongoDB collection associated with a schema, defining the structure of documents that will be stored within it. This article will explain mongoos
4 min read
Mongoose Query getFilter() Method The getFilter() method in Mongoose is a powerful tool used to retrieve the current query filter applied to a query in the Mongoose query system. This method is essential for developers who need to examine or manipulate the query criteria before executing the query. In this article, we will explain t
5 min read