Home > Software design >  How to filter in Mongoose?
How to filter in Mongoose?

Time:09-29

I want to do a filtering operation on id. I want the data with an id of 3 to be skipped.

Example

const id=3;

const data =[
{id:1},
{id:2},
{id:3},
{id:4},
{id:5},
];

// the result i want

[{id:1},{id:2},{id:4},{id:5}]

How can I do this in Mongoose?

CodePudding user response:

Find is your friend. You can apply a condition the the fields you want to search by. In this case it's id. $ne is a query condition, meaning Not Equal to the value given.

Your query will look something like this:

const number = 3
const result = await users.find({ id : { $ne: number } })

CodePudding user response:

yourModelName.find({ id: { $ne: 3 } })

$ne: Not equals to

doc: https://docs.mongodb.com/manual/reference/operator/query/ne/

  • Related