简体   繁体   中英

mongoose query to find based on multiple not equals

i am trying to get results from my userId such that both type != 4 and userType != 1

i tried:

const transaction = {
    userId: userId,
    $and: [{ type: { $ne: 4 } }, { userType: { $ne: 1 } }]
  };
await.Transactions.find(transaction)

but this is giving me empty set.

the below is a snippet of my db data

id: 5ed8dfb1b6c73a69b0adca7d
amount: 150
userId: 5ed5c7fdc25ee07e41a502af
type: 4
userType: 1
recipientId: 5ed8d3a36b34b55f66f62fcd
status:"succeeded"

_id: 5ed987747833b724d9f31829
amount: 1000
userId: 5ed8d3a36b34b55f66f62fcd
type: 4
userType: 2
ChargeId: "tr_1GqSjvKYps1wFgvm0thxzh0p"
ChargeObject: "transfer"
status: "pending"
__v: 0

_id: 5ed987747833b724d9f31841
amount: 1000
userId: 5ed8d3a36b34b55f66f62fcd
type: 3
userType: 1
ChargeId: "ch_1GqSjvKYps1wFgvm0thxzh0p"
ChargeObject: "charge"
status: "pending"
__v: 0

based on your criteria, it should be like:

{ $and:[ {userId: userId},{ type: { $ne: 4 } }, { userType: { $ne: 1 } } ] }

The criteria are type != 4 and userType != 1 , if you want exclude documents for which both of these are true, this statement will be true for documents you want to match:

not(and(type != 4, userType != 1 ))

That seems to be some excessive negation, the above is logically equivalent to:

or(type = 4, userType = 1)

In MQL:

{userId: userId, $or:[ {type: 4}, {userType: 1} ]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM