Querying Date inside $or

I am trying to find the value using the below $OR query in Mongodb but in the value of deactivateddate is wrongly showing as [Object]

var Query = {$or: [

{isActive:false}

,{isActive:true,deactivatedDate: moment().utc().subtract(10,'d').format()}]};

when I am console the query I sent to the mongodb I got this

{$or: [{isActive:false},{isActive:true,deactivatedDate: [Object]}]}

Hi @Vasanthan_K ,

Welcome to MongoDB community.

Is moment return a current date? Maybe try to pass new Date() instead…

Thanks
Pavel

Hi Pavel,

Actually moment().utc().subtract(10,‘d’).format() is returning “2021-07-04T03:24:27Z” outside the $or but returning [object] inside the $OR

Thanks and Regards,
Vasanthan K

But perhaps just moment() returns an object.

Have you tried passing explicitly moment().utc().subtract(10,‘d’).format()?

yes the same piece of moment code is actually working on other part of code for a requirement I have to try it inside $or

Hi @Vasanthan_K!

A few additional questions:

  • When you say that you are in the console, are you using the new MongoDB Shell (mongosh)? This would be the case if you are using the shell embedded in Compass.
  • Does the operation execute correctly (eg it finds the right results) and the main problem here is the fact that the displayed information (of [Object]) is confusing and misleading?

Based on the information here, I suspect that problem is related to how the mongosh utility prints information by default. There is an inspectDepth setting that will begin truncating nested information once a certain depth is reached for overall readability purposes. In your situation, the addition of the $or operator results in the field having additional levels of nesting.

What’s not immediately obvious is why the change in display is being applied to deactivatedDate but not isActive:true which appear to be at the same depth. If the value for both of those fields were a simple data type rather than an object then I would expect both values to be displayed. There are a few things that this could imply, but regardless I think the suggestions below may help.

First, please be sure that you are running the most recent version of mongosh. With it, you can use the following command to check on the setting of the depth inspection:

config.get('inspectDepth')

You can modify the setting using the following command:

config.set('inspectDepth',<INTEGER>)

Documentation for the same can be found here.

Thanks for using MongoDB!

Best,
Chris

1 Like