Mongoose Find - Exclude One Specific Document

Mongoose Find - Exclude One Specific Document



I want to fetch many documents via Schema.find(), but exclude one specific document via its id. Currently, my query looks like:


Schema.find()


Product
.find(
$or: [
'tags': $regex: criteria, $options: 'i' , ,
'name': $regex: criteria, $options: 'i' , ,
],
)
.limit(10)
.exec((err, similar) =>
//...
)



I tried to add $not: _id: someId to the query but that gives me an error, that $not ist not valid.


$not: _id: someId


$not





To use $not you should specify the operator: Product.find( _id: $not: $eq: someId)
– Ron537
Sep 2 at 11:50



$not


Product.find( _id: $not: $eq: someId)




1 Answer
1



Use $ne which stands for not equal


$ne


not equal


Product.find( _id: $ne: someId)



So the whole query would look like


Product
.find(
$and: [
_id: $ne: someId ,
$or: [
'tags': $regex: criteria, $options: 'i' , ,
'name': $regex: criteria, $options: 'i' , ,
],
]
)
.limit(10)
.exec((err, similar) =>
//...
)





is there a way to exclude multiple ids? $ne: someId, someIdAgain doesn't work :/
– Eray T
Nov 8 at 23:15





nevermind, I found it. I used your second example and added more ids that way. thank you.
– Eray T
Nov 8 at 23:18





I am using "mongoose": "^5.3.12", it does not runs.
– 151291
Nov 23 at 12:21


"mongoose": "^5.3.12",



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ