首页 > 解决方案 > 可以匹配填充的嵌套数组吗?

问题描述

是否可以使用带有猫鼬的常规 .findOne 函数对填充数组中的值进行条件匹配?

我有一个模型(Foo),它引用了另一个模型(协议),它有一个我填充的数组。

在填充函数中,我想使用match“查询”仅根据此条件填充数据,但我无法让它工作。甚至可以match在填充数组中使用吗?如果有填充对象,我对匹配没有问题,但对数组没有问题。

我只想isLatestVersion在填充数据设置为 true 的时间/地点填充。任何指针?

我尝试过使用位置运算符、不同的语法等等,但没有任何效果。例如

  'agreementInfo.$.isLatestVersion': true,
  'agreementInfo.$[0].isLatestVersion': {$eq: true},
      etc etc..

填充查询

await Foo.findOne(query)
  .populate([{
        ....
        path: 'agreements.songComposition',
        match: {
          'agreementInfo.isLatestVersion': true,
        },
        .....
      },
   },
   ....
      
  
  

人口模型(Foo)

...
  agreements: {
   ...
    songComposition: { type: mongoose.Schema.Types.ObjectId, ref: 'Agreement' },
  },
  

填充模型(协议)

 ...
   type: String, 
   agreementInfo: [
     { 
       ...
       createdAt: { type: Date, default: Date.now },
       isLatestVersion: Boolean,
       ...
       }
  ]

标签: mongodbmongoose

解决方案


推荐阅读