Home > database >  How to use the distinct method and use the operator $and in MongoDB
How to use the distinct method and use the operator $and in MongoDB

Time:10-18

I would like to get the length of this table when I use the distinct to CustomerId but I would like to get this data until a date knowing that I have a PaymentDate property at my table.

I tried something like

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

But not works and this error appears

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

CodePudding user response:

distinct accept query as a second argument, so you should try this:

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