Filtering date on smart field

Hello again @hugo3m

Sorry for the long delay, this was more time consuming than expected.

MongoDB is not the most convenient backend to do that, as injecting joins is not possible in the filter() and search() handlers.

I set up models similars to yours and wrote an example.

collection('films', {
  actions: [],
  fields: [
    {
      isFilterable: true,
      field: 'myDate',
      type: 'Date',

      // This gets called for each record on screen.
      // To avoid querying the database backend 15 times on list view
      // use this module: https://github.com/graphql/dataloader
      async get(film) {
        const persons = await models.persons
          .find({ preferredFilm: film._id }, ['createdAt']) // Keep only field of interest
          .sort({ createdAt: -1 })
          .limit(1); // limit to one result
        
        return persons.length ? persons[0].createdAt : null;
      },
      async filter({ where }) {
        const docs = await models.persons.aggregate([
          // Keep only docs matching user filter
          { $match: { createdAt: where }},

          // Group to keep only latest doc by documentType1 to match smart field definition
          { $group: { _id: "$preferredFilm", createdAt: { $max: "$createdAt" }}},

          // Keep only _id
          { $project: { _id: 1 }}
        ]);

        return { _id: { $in: docs.map(doc => doc._id) } };
      },
    },
  ],
  segments: [],
});