简体   繁体   English

Mongoose 填充 Null 参考

[英]Mongoose Populate Null Reference

I have two simple models defined in Mongoose, composed of two schema Client and City, I have the property city defined in Client as a ObjectId, ref: 'City', so far so good.我在 Mongoose 中定义了两个简单模型,由两个模式 Client 和 City 组成,我在 Client 中将属性 city 定义为 ObjectId,ref: 'City',到目前为止一切顺利。

If I query for a client and want also to filter by the 'province' property of City, I do this:如果我查询一个客户并且还想按城市的“省”属性进行过滤,我会这样做:

const client = await Client
    .find({ name: "Gérard" })
    .populate([{
        path: 'city',
        model: City,
        match: { province: 'BA' }
    }]);

And the output is just fine:而 output 就好了:

{
  "id": "627264e3ec261a883d42ead9",
  "name": "Gérard",
  "email": "gerard@depardieu.fr",
  "date": "1948-12-27",
  "active": true,
  "city": {
    "id": "627264e3ec261a883d42ead1",
    "name": "Buenos Aires",
    "province": "BA"
  }
}

Howerver, if I input a province code of a nonexistent city:但是,如果我输入一个不存在的城市的省代码:

const client = await Client
    .find({ name: "Gérard" })
    .populate([{
        path: 'city',
        model: City,
        match: { province: 'CA' }
    }]);

It would return me that:它会返回给我:

{
  "id": "627264e3ec261a883d42ead9",
  "name": "Gérard",
  "email": "gerard@depardieu.fr",
  "date": "1948-12-27",
  "active": true,
  "city": null
}

I don't want in this particular scenario, any instance of Client to be returned, and I don't know how to avoid this behavior with Mongoose, a behavior I never had to worry about with Spring Data for instance.在这种特定情况下,我不希望返回任何 Client 实例,而且我不知道如何使用 Mongoose 避免这种行为,例如,我从来不必担心 Spring 数据的这种行为。

Any tips for me?对我有什么建议吗?

Thanks in advance.提前致谢。

I solved it myself, I had to go a little lower level with Mongoose and use aggregates and lookups.我自己解决了它,我不得不将 go 与 Mongoose 结合使用聚合和查找。

const client = await Client.aggregate([
  { 
    $match: { name: "Gérard" } 
  },
  {
    $lookup: {
      from: City.collection.name,
      pipeline: [
        {
          $match: {
            province: 'BA'
          }
        }
      ], as: "city"
    }
  },
  {
     $unwind: "$city"
  },
  {
     $match: {
       city: { $ne: [] }
     }
   }
]);

Expected result:预期结果:

{
  "id": "627264e3ec261a883d42ead9",
  "name": "Gérard",
  "email": "gerard@depardieu.fr",
  "date": "1948-12-27",
  "active": true,
  "city": {
    "id": "627264e3ec261a883d42ead1",
    "name": "Buenos Aires",
    "province": "BA"
  }
}

Witch is ok, client name "Gérard" lives in "Buenos Aires", situated in province "BA".女巫还好,客户名字“Gérard”住在“布宜诺斯艾利斯”,位于“BA”省。

On the other hand:另一方面:

const client = await Client.aggregate([
  { 
    $match: { name: "Gérard" } 
  },
  {
    $lookup: {
      from: City.collection.name,
      pipeline: [
        {
          $match: {
            province: 'CA'
          }
        }
      ], as: "city"
    }
  },
  {
     $unwind: "$city"
  },
  {
     $match: {
       city: { $ne: [] }
     }
   }
]);

Returns nothing, once the city of "Buenos Aires" is not located in province "CA".一旦“布宜诺斯艾利斯”市不在“CA”省,则不返回任何内容。

Notice here the last parameter (as an object) the array passed to Client.aggregate() receives:注意传递给 Client.aggregate() 的数组接收的最后一个参数(作为对象):

{
  $match: {
    city: { $ne: [] }
  }
}

This tells MongoDB that in order to return data city must be not equal to an empty array.这告诉 MongoDB 为了返回数据 city 必须不等于空数组。

And with that the issue is solved.这样问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM