Db.query.findMany() where populate != null

Hello,

I need help to fully understand the db.query with populate !

My tables :

appels : {
id,
espace, (relation to espace)
file
}

espace : {
id,
numero,
type
}

In my code I try to retrieve all the “appels” that have the “espace” with the “numero” 10.

Today I have 2 “espaces” (numero 10 and 17), and I have 2 appels (one for “espace”.“numero” 10 and one for “espace”.“numero” 17)

async getAppels(ctx) {

    let test = await strapi.db.query('api::appel.appel').findMany({
        // select : ['id', 'file'],
        // populate : true
        populate: {
            espace: {
                where: {
                    numero: 10
                }
            },
            file: {
                select: ['name', 'url']
            }
        },
    });


    console.log(test);

    ctx.body = test;
},

When I log data on test i got this :

[
  {
    id: 1,
    createdAt: '2023-11-07T14:55:01.951Z',
    updatedAt: '2023-11-07T14:55:07.199Z',
    publishedAt: '2023-11-07T14:55:07.195Z',
    espace: {
      id: 1,
      numero: 10,
      createdAt: '2023-11-07T14:01:35.469Z',
      updatedAt: '2023-11-07T14:01:38.064Z',
      publishedAt: '2023-11-07T14:01:38.061Z',
      type: 'Appartement'
    },
    file: { name: 'lineo.txt', url: '/uploads/lineo_41bac3f3d6.txt' }
  },
  {
    id: 2,
    createdAt: '2023-11-08T14:32:24.225Z',
    updatedAt: '2023-11-08T14:32:24.833Z',
    publishedAt: '2023-11-08T14:32:24.829Z',
    espace: null,
    file: { name: 'lineo.txt', url: '/uploads/lineo_41bac3f3d6.txt' }
  }
]

Is it possible to modify my query (db.query) or do I have to add one line of my code :

test.filter((t) => t.espace != null);

Thanks

Hey @PepeMax, a minor change required for your query.

I suggest you to use the below query for accurate results.

const query = await strapi.db.query("api::appel.appel").findMany({
  where: {
    $not: {
      espace: null,
    }
  },
  populate: {
    espace: {
      where: {
        numero: 10
        // numero: { $eq: 10 } Other way to use where query for the above
      }
    },
    file: {
      select: ['name', 'url']
    }
  },
});