# Mongoose - Smart Field filter "where" parameter not properly created for some Date operators

**URL:** https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883
**Category:** Help me!
**Created:** [November 29, 2023, 4:56pm UTC](https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883 "2023-11-29T16:56:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yoad\_Snapir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/yoad_snapir/32/101_2.png) [@Yoad\_Snapir](https://community.forestadmin.com/u/Yoad_Snapir)
#### Post date: [November 29, 2023, 4:56pm UTC](https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883/1 "2023-11-29T16:56:31Z")

</div>

## Feature(s) impacted

Smart Field of type Date will not process filtering properly in all cases.

## Observed behavior

Creating a Date smart field, relying on the “where” argument will provide wrong “String” dates when the operator is not an exclusive “Date” operator.  
The expected “where” structure is an object where the value is a Date object combined into a valid formatted query.  
For a mongodb - that would be a valid mongoose query expression.

## Expected behavior

Date smart fields should properly support filtering using the “where” argument.

## Failure Logs

Here is a details explanation of the problem.

Let’s assume a basic mongoose schema:

```js
{ modified_at: Date }

```

A Smart Field is defined of type “Date” for example

```js
{
    field: 'funny_modified_at',
    type: 'Date',
    isFilterable: true,
    get: (record) => {
      return record.modified_at;
    },
    filter({ condition, where }) {
      return {
        'modified_at': where,
      };
    },
}

```

### The good

Client sends a filter on the Smart Field the mongoose express backend.  
For example:

```json
{"field":"funny_modified_at","operator":"yesterday","value":null}

```

The backend processes this properly!

The “filter” method on the smart field gets the following “where” argument:

```json
{ '$gte': 2023-11-27T22:00:00.000Z, '$lte': 2023-11-28T21:59:59.999Z }

```

Note - Those is a valid ISODate() time values. This works since “yesterday” is considered a dedicated “Date” operator as defined [here](https://github.com/ForestAdmin/forest-express/blob/37c36e4ccac3fd69250301d55b85c5463f017490/src/services/base-operator-date-parser.js#L39).

### The Bad

Now the client send the following filter:

```json
{"field":"funny_modified_at","operator":"before","value":"2023-11-21T22:00:00.000Z"}

```

The “filter” method on the smart field gets the following “where” argument:

```json
{ '$lt': '2023-11-21T22:00:00.000Z' }

```

Note - this is a string formatted Date - not a Date object.

### The Ugly

Why is this happening?  
The “where” input is generated within the `forest-express` lib in `base-filters-parser.js` within the `parseCondition` function [here](https://github.com/ForestAdmin/forest-express/blob/37c36e4ccac3fd69250301d55b85c5463f017490/src/services/base-filters-parser.js#L33C6-L33C6).

```javascript
const where = await formatCondition(condition, true);
const formattedCondition = await fieldFound
  .filter({
    where,
    condition,
  });

```

`formatCondition` is injected from the implementation layer - in my case `forest-express-mongoose` in `filter-parse.js` and defined [here](https://github.com/ForestAdmin/forest-express-mongoose/blob/e865a362bcae1b5e8b6db4db4675a9a61c5cf935/src/services/filters-parser.js#L46C30-L46C30) like this:

```javascript
this.formatCondition = async (condition, isSmartField = false) => {
    if (isSmartField) {
      return this.formatOperatorValue(
        condition.field,
        condition.operator,
        condition.value,
      );
    }
...
  };

```

In this scenario, it is called with `isSmartField = true` so that “if” is entered.

At the begging of `formatOperatorValue` we check for the special “Date” operators like so:

```javascript
this.formatOperatorValue = async (field, operator, value) => {
    if (this.operatorDateParser.isDateOperator(operator)) {
      return this.operatorDateParser.getDateFilter(operator, value);
    }

```

Here something like “yesterday” would enter the if and will “save the day” - prevent the bug - the “value” will be formatted as a Date object inside `getDateFilter`.

But! If the operator is something like `before` it is not considered an exclusive Date operator and will skip this if clause.

Then - it does the following:

```javascript
const parseFct = await this.getParserForField(field);

```

And within `getParserForField` there is:

```javascript
this.getParserForField = async (key) => {
    const [fieldName, subfieldName] = key.split(':');

    const field = SchemaUtils.getField(modelSchema, fieldName);

    if (!field) {
      throw new InvalidFiltersFormatError(`Field '${fieldName}' not found on collection '${modelSchema.name}'`);
    }

    const fieldPath = subfieldName ? `${fieldName}.${subfieldName}` : fieldName;
    const fieldType = utils.getNestedFieldType(model.schema, fieldPath);

    // fieldType is empty here for Smart Fields!
    if (!fieldType) return (val) => val;

```

Which tries to infer the field type from the `utils.getNestedFieldType` function.  
This does not return Date, instead it tries to infer only from the mongoose schema which of course does not include the Smart Field type.  
**This is the root cause of the bug.**

Looking at [this](https://community.forestadmin.com/t/filtering-date-on-smart-field/2976) post from 2021 - it seems like “where” was working as expected back then.

Indeed this [commit](https://github.com/ForestAdmin/forest-express-mongoose/commit/2bb7ee828bf2591fe4b7360c68a78210e2405773) from 2 years ago broke things changing how field types are inferred ignoring the Smart Field case.

## Context

- Project name: Any Project
- Team name: Any Team
- Agent (forest package) name & version: mongoose agent 8+9
- Database type: mongodb

---

<div class="post-metadata">

### Author: ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)
#### Post date: [November 30, 2023, 9:50am UTC](https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883/2 "2023-11-30T09:50:40Z")

</div>

Hi @Yoad_Snapir,

I will try to reproduce you issue and get back to you. Thanks a lot for all those details ti really simplify my life 😃

---

<div class="post-metadata">

### Author: ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)
#### Post date: [November 30, 2023, 10:10am UTC](https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883/3 "2023-11-30T10:10:51Z")

</div>

Hello @Yoad_Snapir, and thanks for this detailed report.  
I just reproduced the issue and I am now working on a resolution.

I will keep you posted.

---

<div class="post-metadata">

### Author: ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)
#### Post date: [December 6, 2023, 8:03am UTC](https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883/4 "2023-12-06T08:03:17Z")

</div>

Hello @Yoad_Snapir ,

A fix for this issue has been released in forest-express-mongoose 9.3.13  
Please upgrade and let me know if it works as expected for you 🙏

Regards,  
@anon7311026

---

<div class="post-metadata">

### Author: ![Yoad\_Snapir](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/yoad_snapir/32/101_2.png) [@Yoad\_Snapir](https://community.forestadmin.com/u/Yoad_Snapir)
#### Post date: [December 10, 2023, 9:52am UTC](https://community.forestadmin.com/t/mongoose-smart-field-filter-where-parameter-not-properly-created-for-some-date-operators/6883/5 "2023-12-10T09:52:28Z")

</div>

This solved the problem.  
Required upgrade to V9 so I will perform tests before deploying.  
Thanks!
