lucid-mongo icon indicating copy to clipboard operation
lucid-mongo copied to clipboard

Fixed count method: now returns 0 instead of null

Open richie3366 opened this issue 6 years ago • 10 comments

The basic usage of the count method does return null instead of 0 (zero) when no document corresponds to the query.

This fix allows developers to use it without encountering unexpected results &/or errors.

Warning: it can lead to breaking changes if developers (who have noticed the behavior) have chosen to test the nullity (e.g.: ==[=] null) of its return value.

richie3366 avatar Jan 05 '19 17:01 richie3366

Coverage Status

Coverage decreased (-0.08%) to 87.639% when pulling 4a285da73e120270b651e1dd1773dfe8c55b8171 on Pickaw:patch-2 into dd9d100b95992da4c7f95f2906e41115201948dd on duyluonglc:develop.

coveralls avatar Jan 05 '19 17:01 coveralls

Coverage Status

Coverage decreased (-0.08%) to 87.639% when pulling 4a285da73e120270b651e1dd1773dfe8c55b8171 on Pickaw:patch-2 into dd9d100b95992da4c7f95f2906e41115201948dd on duyluonglc:develop.

coveralls avatar Jan 05 '19 17:01 coveralls

That would be a very nice fix to prevent additional/useless conditions! Any plan to merge this PR @duyluonglc? 🙂

HapLifeMan avatar Feb 15 '19 22:02 HapLifeMan

Sorry for late response How about the case query count with group by?

const userCount = await User.count('status')

can resolve group by case and make test script for them?

duyluonglc avatar Feb 21 '19 16:02 duyluonglc

It seems it's already tested here https://github.com/duyluonglc/lucid-mongo/blob/f42b8901824d61d30caa86e9c749655fdb050fcc/test/unit/lucid.spec.js#L1824-L1827 and here https://github.com/duyluonglc/lucid-mongo/blob/4e4ae36cb4cdefa47a14654c21dd9280f5d5fac7/test/unit/database.spec.js#L160-L161

But, in the case no document matches the query, the returned value is an empty array (in both branches of the merge ; my changes didn't affect the group by count).

If we would want to adapt it consistently, it would imply to make it return an array with a single { _id: null, count: 0} element.

What do you think about also changing it?

richie3366 avatar Feb 21 '19 16:02 richie3366

@richie3366 can you make the test script with zero response for both cases?

duyluonglc avatar Feb 21 '19 17:02 duyluonglc

Do you mean that it also has to return 0 with the group by query*? (* in case no document is matched against the query)

richie3366 avatar Feb 23 '19 13:02 richie3366

@duyluonglc Any update?

HapLifeMan avatar Mar 24 '19 13:03 HapLifeMan

Little reminder about my question: I just need to know if you want me to make the groupBy count call to return 0 (thus not a { _id: null, count: 0})? (only in the case of no document is matched against the query)

If you need more explanations &/or examples, feel free to ask. Thanks

richie3366 avatar Apr 07 '19 14:04 richie3366

Hey @duyluonglc, any update about this? Thanks!

HapLifeMan avatar Aug 27 '19 10:08 HapLifeMan