如何根据OR条件查询猫鼬

时间:2020-05-25 11:20:15

标签: mongodb mongoose mongoose-schema

我有一个交易集合,我需要从猫鼬那里查询这个

    _id: 5ecba0d446d0354084ad0b89
    amount: 3
    userId: 5ec3285cc7762963c88db765
    type: 4
    userType: 1
    recipientId: 5ec328f2c7762963c88db768
    status: "succeeded"
    createdAt: 2020-05-25T10:41:24.449+00:00
    updatedAt: 2020-05-25T10:41:24.449+00:00
    __v: 0


    _id: 5ecba0d446d0354084ad0b92
    amount: 4
    userId: 5ec3285cc7762963c88db888
    type: 4
    userType: 1
    recipientId: 5ec3285cc7762963c88db765
    status: "succeeded"
    createdAt: 2020-05-25T10:41:24.449+00:00
    updatedAt: 2020-05-25T10:41:24.449+00:00
    __v: 0


    _id: 5ecba0d446d0354084ad0b97
    amount: 8
    userId: 5ec3285cc7762963c88db332
    type: 4
    userType: 1
    recipientId: 5ec328f2c7762963c88db589
    status: "succeeded"
    createdAt: 2020-05-25T10:41:24.449+00:00
    updatedAt: 2020-05-25T10:41:24.449+00:00
    __v: 0

如何查询此信息,以便我可以根据以下条件获取交易

userId = 5ec3285cc7762963c88db765 or recipientId = 5ec3285cc7762963c88db765
and type = 4
and userType = 1

1 个答案:

答案 0 :(得分:2)

使用$or$and运算符

model.find({
   $or: [ 
     { userId: 5ec3285cc7762963c88db765 },
     { recipientId: 5ec3285cc7762963c88db765 }
   ],
   $and: [ { type: 4 }, { userType: 1 } ]
});