Mongoose Query() Function

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: 
You can visit the link to Install mongoose module. You can install this package by using this command. 
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 connectionmongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {    useNewUrlParser: true,    useCreateIndex: true,    useUnifiedTopology: true});  // User modelconst 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 connectionmongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {    useNewUrlParser: true,    useCreateIndex: true,    useUnifiedTopology: true});  // User modelconst 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 
Mongoose
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between promise and async await in Node.js
JWT Authentication with Node.js
Installation of Node.js on Windows
Node.js forEach() function
How to use an ES6 import in Node.js?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React