Open In App

Mongoose Query.prototype.nor() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.nor() method of the Mongoose API is used on the Query objects. It allows us to perform the logical NOR operation on the result set. Using this method we can provide the query expression and the NOR conditions in the form of array. Let us understand nor() method using an example.

Syntax:

query.nor( array );

Parameters: This method accepts a single parameter as described below:

  • array: It is used to specify the array of conditions.

Return Value: This method returns the Query object.

Setting up Node.js Mongoose Module:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Database Structure: The database structure will look like this, the following database present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection nor() method. In this example, we are fetching all the document except document that has 9 as a value for rollNumber field.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
const StudentModel = 
    connectionObject.model('Student', studentSchema);
  
const query = StudentModel.find();
  
query.nor([{ rollNumber: 9 }]);
  
query.exec((error, result) => {
    if (error) {
        console.log("Error -", error);
    } else {
        console.log("Result -", result);
    }
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Result - [
  {
    _id: new ObjectId("63a4a98407370cdcd1961b1a"),
    name: 'Student3',
    age: 17,
    rollNumber: 178,
    __v: 0
  },
  {
    _id: new ObjectId("63a4a9a207370cdcd1961b2c"),
    name: 'Student2',
    age: 18,
    rollNumber: 176,
    __v: 0
  }

Example 2: The below example illustrates the basic functionality of the Mongoose Connection nor() method. In this example, we are fetching all the document except document that has “Student1” as a value for name field and 17 as a value for age field.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
const StudentModel = connectionObject.model('Student', studentSchema);
  
const query = StudentModel.find();
  
query.nor([{ name: "Student1" }, { age: 17 }]);
  
query.then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error)
});


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
  {
    _id: new ObjectId("63a4a9a207370cdcd1961b2c"),
    name: 'Student2',
    age: 18,
    rollNumber: 176,
    __v: 0
  }
]

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-nor



Last Updated : 13 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads