Muthukumar Marichamy
Muthukumar Marichamy

Reputation: 1214

How to Apply not in condition inside the populate in mongoose with express?

How to apply "not in" query inside populate as below in Mongoose with Express js ?

Users
  .find({ user_enabled: true })
  .populate({
    path: 'books',
    match: { 'language': 'en' }, 

[ Not in en language ]

  })
  .exec()

Upvotes: 2

Views: 410

Answers (3)

Raj Adroit
Raj Adroit

Reputation: 3878

You may use $nin Comparison Query Operator for this

Example:

Users
  .find({ user_enabled: true })
  .populate({
    path: 'books',
    match: { 'language': 'en' }, 
    match: { "language": { "$nin": ['xx','yy'] } }  // <= See here
  })
  .exec()

Upvotes: 2

Ivan Vasiljevic
Ivan Vasiljevic

Reputation: 5718

Have you tried $ne operator?

So in your case:

Users
  .find({ user_enabled: true })
  .populate({
    path: 'books',
    match: { language: { $ne: "en" } },
})
.exec() 

Upvotes: 1

C&#233;dric De Dycker
C&#233;dric De Dycker

Reputation: 81

Users
  .find({ user_enabled: true })
  .populate({
    path: 'books',
    match: { 'language': {$not: 'en'} }, })
  .exec()

Upvotes: 1

Related Questions