Open In App

Mongoose Query() Function

Last Updated : 10 Sep, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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
 


Next Article

Similar Reads