MongoDB填充后查找

时间:2019-11-26 11:29:42

标签: mongodb aggregate lookup

我有两个集合:服务器连接。下面是一个示例。

问题:我需要在 inputConnections 中获得一个具有 https 连接的服务器

示例查询:

Server.find({}).populate('inputStreams').findOne({inputConnections: { $elemMatch: { type: 'https' } }});

我尝试执行以下查询,但结果为空

aggregate(
      [
        {
          $lookup: {
            from: 'Connections',
            localField: 'inputConnections',
            foreignField: '_id',
            as: 'inputConnections',
          },
        },
        { $match: { inputConnections: { $elemMatch: { type: 'https' } } } },
      ],
      (err, result) => {
      },
    )

示例数据:

服务器:

{
  _id: "5ddcf58698fc3367fa357c1d"
  inputConnections: [
    0: ObjectId("5ddd05ba0bbb05288c1df890")
    1: ObjectId("5ddd09702ed7553ec33dad80")
  ]
}
{
  _id: "5ddd09f12ed7553ec33dad81"
  inputConnections: [
    0: ObjectId("5ddd09f22ed7553ec33dad82")
    1: ObjectId("5ddd09f12ed7553ec33dad83")
  ]
}

连接:

{
  _id: "5ddd05ba0bbb05288c1df890"
  typeConnection: "http",
  description: "Lorem ipsum"
},
{
  _id: "5ddd09702ed7553ec33dad80"
  typeConnection: "https",
  description: "Lorem ipsum"
},
{
  _id: "5ddd09f22ed7553ec33dad82"
  typeConnection: "customTypeCOnnection",
  description: "Lorem ipsum"
},
{
  _id: "5ddd09f12ed7553ec33dad83"
  typeConnection: "secondCustomTypeCOnnection",
  description: "Lorem ipsum"
}

需要结果:

{
  _id: "5ddcf58698fc3367fa357c1d"
  inputConnections: [
    0: ObjectId("5ddd05ba0bbb05288c1df890")
    1: ObjectId("5ddd09702ed7553ec33dad80")
  ]
}

0 个答案:

没有答案