mongoose 如何从MongoDb中的不同文档中获取特定数组对象

rwqw0loc  于 2023-03-30  发布在  Go
关注(0)|答案(1)|浏览(146)

我遇到了一个问题。问题是,我有多个集合,每个集合都有维度字段,其中包含不同维度的对象数组。集合看起来像下面这样。

现在我只需要从slug数组中返回特定的对象,id可以来自多个文档。
我一直在使用下面的查询来过滤,但它返回给我整个文档。

db.dimensions.find({
    "dimension.dimensionChildSlug": { $in: ["spring", "formal"] } 
}).select({ dimension: 1 })

但预期的结果应该是这样的,只有数组中与slug匹配的项才会被检索。

[
    {
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423b71df226c91a55d756bb"
      },
      "dimensionChild": "Spring",
      "dimensionChildToLower": "spring",
      "dimensionChildSlug": "spring"
    },
{
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423cde4f4db0d397b22390b"
      },
      "dimensionChild": "Wedding",
      "dimensionChildToLower": "wedding",
      "dimensionChildSlug": "wedding"
    }
]

请找到收藏

[{
  "_id": {
    "$oid": "6423b6faf226c91a55d756b9"
  },
  "isDeleted": false,
  "isArchived": false,
  "dimensionType": "Collections",
  "dimensionTypeSlug": "collections",
  "dimensionTypeToLower": "collections",
  "dimension": [
    {
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423b71df226c91a55d756bb"
      },
      "dimensionChild": "Spring",
      "dimensionChildToLower": "spring",
      "dimensionChildSlug": "spring"
    },
    {
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423b723f226c91a55d756bc"
      },
      "dimensionChild": "Summer",
      "dimensionChildToLower": "summer",
      "dimensionChildSlug": "summer"
    }
  ],
  "createdAt": {
    "$date": "2023-03-29T03:56:42.370Z"
  },
  "updatedAt": {
    "$date": "2023-03-29T03:57:23.419Z"
  },
  "__v": 0
},{
  "_id": {
    "$oid": "6423cddbf4db0d397b22390a"
  },
  "isDeleted": false,
  "isArchived": false,
  "dimensionType": "Occassions",
  "dimensionTypeSlug": "occassions",
  "dimensionTypeToLower": "occassions",
  "dimension": [
    {
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423cde4f4db0d397b22390b"
      },
      "dimensionChild": "Wedding",
      "dimensionChildToLower": "wedding",
      "dimensionChildSlug": "wedding"
    },
    {
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423ce0af4db0d397b22390c"
      },
      "dimensionChild": "Business Casual",
      "dimensionChildToLower": "business casual",
      "dimensionChildSlug": "business-casual"
    },
    {
      "isDeleted": false,
      "isArchived": false,
      "_id": {
        "$oid": "6423ce18f4db0d397b22390d"
      },
      "dimensionChild": "Formal",
      "dimensionChildToLower": "formal",
      "dimensionChildSlug": "formal"
    }
  ],
  "createdAt": {
    "$date": "2023-03-29T05:34:19.300Z"
  },
  "updatedAt": {
    "$date": "2023-03-29T05:35:20.294Z"
  },
  "__v": 0
}]

有没有人能帮我解决这个问题。
谢谢。

u0sqgete

u0sqgete1#

我想你应该使用$filter

db.collection.aggregate([
  {$match: {
      "dimension.dimensionChildSlug": {$in: ["spring", "formal"]}
  }},
  {$project: {
      dimension: {$filter: {
          input: "$dimension",
          cond: {$in: ["$$this.dimensionChildSlug", ["spring", "formal"]]}
      }}
  }}
])

了解它在playground example上的工作原理

相关问题