What is equals() in Mongoose?

equals() is a query function that is used to specify the complementary comparison value for paths or fields that are specified with where() in Mongoose.

In other words, this function returns the document or documents that match a field specified with where, with the corresponding value passed with the equal function.

Syntax


query.equals()
// OR
Query.prototype.equals()

Parameter

The parameter the function takes is an Object value.

Return value

The function returns a query object.

Code

In the example below, we create a schema, a query, and use the equals() function.

// import mongoose
const mongoose = require("mongoose");
// creating a Schema with mongoose
let Person = mongoose.model("Person", new mongoose.Schema({
name: String,
age: Number
})
)
// make a query and use the `equal()` method
const persons = await Person.find().where("age").equals(15)
/*
OUTPUT : persons is an arrary of objects
that has "age" as 15
*/

Explanation

In the above code, persons is an array of Mongoose documents that have a value of 15 for the age field.

Free Resources