MongoDB聚合:返回包含匹配值的数组中的对象。

huangapple go评论51阅读模式
英文:

MongoDB Aggregation: Return the object in an array which contains a matching value

问题

在我的MongoDB聚合管道中,我想从数据中检索与数字 "9999933333" 匹配的对象(见下文)。

对于这个数字 "9999933333",我希望结果如下:

'matchingObjects': [
  {
    id: 'efg',
    phoneNumbers: ['9999933333','9999944444']
  },
  {
    id: 'hij',
    phoneNumbers: ['9999933333','9999955555']
  }
]

以下是数据(经过前几个阶段之后的数据):

{
 id: 123
 contactsOfAppUsers:[
   {
    id:'abc',
    contactsArray: ['9999911111','9999922222']
   },
   {
    id:'efg',
    contactsArray: ['9999933333','9999944444']
   },
   {
    id:'hij',
    contactsArray: ['9999955555','9999933333']
   }
 ]
}

我尝试了以下操作,但它返回布尔值,不是我想要的结果。

db.phNumbers.aggregate([
  {// Previous stage},
  {
    $addFields: {
      'matchingObjects': {
        '$map': {
          'input': '$contactsOfAppUsers',
          'as': 'cc',
          'in': {
            '$in': [
              '9999933333', '$$cc.contactsArray'
            ]
          }
        }
      }
    }
  },
])
英文:

In my MongoDB aggregation pipeline, I want to retrieve the matching objects for a number from the data (see below)

For this number "9999933333", I would like the result like this:

  'matchingObjects':[ 
    {
      id:'efg',
      phoneNumbers: ['9999933333','9999944444']
   },
   {
      id:'hij',
      phoneNumbers: ['9999933333','9999955555']
   }
  ]

Here is the Data (after previous stages):

{
 id: 123
 contactsOfAppUsers:[
   {
    id:'abc',
    contactsArray: ['9999911111','9999922222']
   },
   {
    id:'efg',
    contactsArray: ['9999933333','9999944444']
   },
   {
    id:'hij',
    contactsArray: ['9999955555','9999933333']
   }
 ]
}

I tried this, which gives boolean values which is not what I want.

db.phNumbers.aggregate([
  {// Previous stage},
  {
    $addFields: {
      'matchingObjects': {
        '$map': {
          'input': '$contactsOfAppUsers',
          'as': 'cc',
          'in': {
            '$in': [
              '9999933333','$$cc.contactsArray'
            ]
          }
        }
      }
    }
  },
])

答案1

得分: 1

你可以尝试这个:

db.collection.aggregate({
  $project: {
    _id: 0,
    matchingObjects: {
      $filter: {
        input: "$contactsOfAppUsers",
        as: "numbers",
        cond: {
          $in: [
            "9999933333",
            "$$numbers.contactsArray"
          ]
        }
      }
    }
  }
})

参考:https://mongoplayground.net/p/Q5xlO1ZOMcb

编辑

如果你真的想要将字段重命名为 phoneNumbers,可以尝试这个:

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      matchingObjects: {
        $filter: {
          input: "$contactsOfAppUsers",
          as: "numbers",
          cond: {
            $in: [
              "9999933333",
              "$$numbers.contactsArray"
            ]
          }
        }
      }
    }
  },
  {
    $addFields: {
      matchingObjects: {
        $map: {
          input: "$matchingObjects",
          as: "matchingObjects",
          in: {
            phoneNumbers: "$$matchingObjects.contactsArray",
            id: "$$matchingObjects.id"
          }
        }
      }
    }
  }
])

参考:https://mongoplayground.net/p/cXkFo59YdMy

英文:

You should try this:

db.collection.aggregate({
  $project: {
    _id: 0,
    matchingObjects: {
      $filter: {
        input: "$contactsOfAppUsers",
        as: "numbers",
        cond: {
          $in: [
            "9999933333",
            "$$numbers.contactsArray"
          ]
        }
      }
    }
  }
})

See: https://mongoplayground.net/p/Q5xlO1ZOMcb

EDIT

If you really want to rename the field to phoneNumbers, try this:

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      matchingObjects: {
        $filter: {
          input: "$contactsOfAppUsers",
          as: "numbers",
          cond: {
            $in: [
              "9999933333",
              "$$numbers.contactsArray"
            ]
          }
        }
      }
    }
  },
  {
    $addFields: {
      matchingObjects: {
        $map: {
          input: "$matchingObjects",
          as: "matchingObjects",
          in: {
            phoneNumbers: "$$matchingObjects.contactsArray",
            id: "$$matchingObjects.id"
          }
        }
      }
    }
  }
])

See: https://mongoplayground.net/p/cXkFo59YdMy

huangapple
  • 本文由 发表于 2023年2月23日 21:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定