如何在MongoDB中使用distinct方法和运算符$and



当我使用与CustomerId不同的时,我想获得此表的长度,但我希望在知道表中有PaymentDate属性的日期之前获得此数据。

我试过类似的东西

db.getCollection('PaymentByCreditCard')
.find({
$and: [
{"PaymentDate": { $lte: ISODate("2022-10-03T03:00:00.000Z")}},
]
})
.distinct("CustomerId")
.length

但不起作用,此错误出现

Uncaught exception: TypeError: db.getCollection(...).find(...).distinct is not a function :

distinct接受查询作为第二个参数,因此您应该尝试以下操作:

db.getCollection('PaymentByCreditCard')
.distinct("CustomerId", { "PaymentDate": { $lte: ISODate("2022-10-03T03:00:00.000Z")}})

最新更新