Using $or and $ne Operators Together in MongoDB Query

Using $or and $ne Operators Together in MongoDB Query



I am trying to run a MongoDB query to return all documents that don't match one value OR another. After consulting the documentation, I see this example:


$or: [ quantity: $lt: 20 , price: 10 ]



So this is what I tried:


$or: [ paymentType: $ne: "full" , $ne: "partial" ]



This results in an error:



'unknown top level operator: $ne'



I also tried this:


$or: [ paymentType: $ne: "full" , paymentType: $ne: "partial" ]



... but this simply returns all documents, with no filtering done on the "paymentType" property.



What's the correct syntax here?





Use $nin for match on single field. Something like paymentType:$nin:["full", "partial"]
– Veeram
Aug 21 at 13:48



$nin


paymentType:$nin:["full", "partial"]




3 Answers
3



Your third version of code syntax is correct.



But the logic seems wrong.



Your query translated to:


paymentType != full or paymentType != partial



This actually always hold true, (since one paymentType value cannot be full and partial at the same time.)


paymentType


full


partial



Update



To clarify, what I'm trying to query -- in plain English -- is to return all results where "paymentType" is not "full" or "partial".



In this case you should use $and instead. or $nin is also good.


$and


$nin



Remember this law:


not (p or q) <=> (not p) and (not q)





To clarify, what I'm trying to query -- in plain English -- is to return all results where "paymentType" is not "full" or "partial".
– Muirik
Aug 21 at 13:51





Then use must use an $and instead. Or use $nin like suggested above
– Mạnh Quyết Nguyễn
Aug 21 at 13:52


$and


$nin





This is probably the way to go, as suggested above: paymentType:$nin:["full", "partial"]
– Muirik
Aug 21 at 13:53





Good point. If I were to use an operator here, it should be $and, not $or.
– Muirik
Aug 21 at 13:54





Actually, even better is this, the $nor operator: $nor: [ paymentType: $ne: "full" , paymentType: $ne: "partial" ]
– Muirik
Aug 21 at 14:08




So, thanks to a commenter for pointing out that I could either use $nin or $and but not $or. Another option, which I find the most intuitive in this particular case, is the $nor operator, like so:


$nin


$and


$or


$nor


$nor: [ paymentType: $ne: "full" , paymentType: $ne: "partial" ]



This will return all documents that don't match either of these values.



If you need to find all documents which do not match those two values only why don't you simply change the $or to $and? Easiest and shortest is to use the $nin as already suggested but not sure why complicate it with $nor/$or etc.


all


$or


$and


$nin


$nor


$or


$and: [ paymentType: $ne: "full" , paymentType: $ne: "partial" ]



will select all documents that don't match either of these values. It reads and understands much easier then the double negation you end up with $nor no?


$nor



It says give me all paymentTypes which are not full and not partial. Same if you use the $nin.


full


partial


$nin





I guess it's partly a subjective preference. For me, mentally, $nor equates more closely with the logic in my mind in this case than $and -- hence why I made the mistake in using $or instead of $and to begin with.
– Muirik
Aug 21 at 17:41






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

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

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

Node.js puppeteer - Use values from array in a loop to cycle through pages