检查当前实例的ID是否在带有Mongoose聚合的子阵列中

发布于 2025-02-05 22:11:10 字数 2305 浏览 2 评论 0原文

db.Product.aggregate([
  {
    $match: {
      $or: [
        {
          "message": {
            $regex: "he"
          }
        },
        {
          "field3": {
            $regex: "s"
          }
        }
      ]
    }
  },
  {
    $lookup: {
      from: "User",
      localField: "_id",
      foreignField: "favorite",
      as: "join"
    }
  },
  {
    $addFields: {
      favorited: {
        $cond: [
          {
            $in: [
              "$_id",
              "$join.favorite"
            ]
          },
          true,
          false
        ]
      }
    }
  }
])

我对为什么这不起作用感到困惑。它应该返回真实,但我没有得到预期的结果。我可以使其与$函数一起使用,但是我无法使用$函数,因为我使用的mongoDB版本为4.2而不是4.4,这是$ function所需的。

[
  {
    "_id": "60508eeb52a2ab598a013bb9",
    "favorited": false,
    "field1": false,
    "field2": 1,
    "field3": "something",
    "join": [],
    "message": "hello"
  },
  {
    "_id": "60508f4152a2ab598a013bbb",
    "favorited": true,
    "field1": false,
    "field2": 1,
    "field3": "s1",
    "join": [
      {
        "_id": ObjectId("60508f4152a2ab598a013bbb"),
        "aId": ObjectId("60508f2852a2ab598a013bba"),
        "favorite": [
          "60508f4152a2ab598a013bbb",
          "60508f4552a2ab598a013bbc"
        ]
      },
      {
        "_id": ObjectId("60508f4552a2ab598a013bbc"),
        "aId": ObjectId("60508f2852a2ab598a013bba"),
        "favorite": [
          "60508f4152a2ab598a013bbb",
          "60508f4552a2ab598a013bbc"
        ]
      },
      {
        "_id": ObjectId("605090a452a2ab598a013bbd"),
        "aId": ObjectId("60508eeb52a2ab598a013bb9"),
        "favorite": [
          "60508f4152a2ab598a013bbb",
          "60508f4552a2ab598a013bbc"
        ]
      }
    ],
    "message": "hello"
  },
  {
    "_id": "60508f2852a2ab665a013bba",
    "favorited": false,
    "field1": false,
    "field2": 1,
    "field3": "s1",
    "join": [],
    "message": "hello"
  }
]

我期望上述结果。

https://mongoplayground.net/p/km80poz8rsq

表达式可以包括字段路径,文字,系统变量, 表达对象和表达操作员。表情可以 嵌套。

< .com/docs/Manual/Meta/gentregation-Quick-reference/#std-label-aggregation-expressions

db.Product.aggregate([
  {
    $match: {
      $or: [
        {
          "message": {
            $regex: "he"
          }
        },
        {
          "field3": {
            $regex: "s"
          }
        }
      ]
    }
  },
  {
    $lookup: {
      from: "User",
      localField: "_id",
      foreignField: "favorite",
      as: "join"
    }
  },
  {
    $addFields: {
      favorited: {
        $cond: [
          {
            $in: [
              "$_id",
              "$join.favorite"
            ]
          },
          true,
          false
        ]
      }
    }
  }
])

I am confused as to why this doesn't work. It should return true, but I am not getting the expected result. I can make it work with $function, but I can't use $function since the MongoDB version I am using is 4.2 and not 4.4, which is required for $function.

[
  {
    "_id": "60508eeb52a2ab598a013bb9",
    "favorited": false,
    "field1": false,
    "field2": 1,
    "field3": "something",
    "join": [],
    "message": "hello"
  },
  {
    "_id": "60508f4152a2ab598a013bbb",
    "favorited": true,
    "field1": false,
    "field2": 1,
    "field3": "s1",
    "join": [
      {
        "_id": ObjectId("60508f4152a2ab598a013bbb"),
        "aId": ObjectId("60508f2852a2ab598a013bba"),
        "favorite": [
          "60508f4152a2ab598a013bbb",
          "60508f4552a2ab598a013bbc"
        ]
      },
      {
        "_id": ObjectId("60508f4552a2ab598a013bbc"),
        "aId": ObjectId("60508f2852a2ab598a013bba"),
        "favorite": [
          "60508f4152a2ab598a013bbb",
          "60508f4552a2ab598a013bbc"
        ]
      },
      {
        "_id": ObjectId("605090a452a2ab598a013bbd"),
        "aId": ObjectId("60508eeb52a2ab598a013bb9"),
        "favorite": [
          "60508f4152a2ab598a013bbb",
          "60508f4552a2ab598a013bbc"
        ]
      }
    ],
    "message": "hello"
  },
  {
    "_id": "60508f2852a2ab665a013bba",
    "favorited": false,
    "field1": false,
    "field2": 1,
    "field3": "s1",
    "join": [],
    "message": "hello"
  }
]

I am expecting the above result.

https://mongoplayground.net/p/kM80POZ8rSq

Expressions can include field paths, literals, system variables,
expression objects, and expression operators. Expressions can be
nested.

https://www.mongodb.com/docs/manual/meta/aggregation-quick-reference/#std-label-aggregation-expressions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2025-02-12 22:11:10

如果您尝试读取fafe> fairm in in中的

{
  "join": [
    "favorite": [
      "value1",
      "value2"
    ]
  ]
}

值:

[ ["value1", "value2"] ]

您需要在通过$降低进行比较之前,在执行$之前将数组弄平。

{
  $reduce: {
    input: "$join",
    initialValue: [],
    in: {
      $concatArrays: [
        "$value",
        "$this.favorite"
      ]
    }
  }
}

完成查询

db.testA.aggregate([
  {
    $match: {
      $or: [
        {
          "message": {
            $regex: "he"
          }
        },
        {
          "field3": {
            $regex: "s"
          }
        }
      ]
    }
  },
  {
    $lookup: {
      from: "testB",
      localField: "_id",
      foreignField: "favorite",
      as: "join"
    }
  },
  {
    $addFields: {
      favorite: {
        $cond: [
          {
            $in: [
              "$_id",
              {
                $reduce: {
                  input: "$join",
                  initialValue: [],
                  in: {
                    $concatArrays: [
                      "$value",
                      "$this.favorite"
                    ]
                  }
                }
              }
            ]
          },
          true,
          false
        ]
      }
    }
  }
])

示例mongo playground

If you try to read the value for favorite in join, it returns:

{
  "join": [
    "favorite": [
      "value1",
      "value2"
    ]
  ]
}

So when you query with join.favorite, it returns a nested array:

[ ["value1", "value2"] ]

You need to flatten the array before performing $in comparison via $reduce.

{
  $reduce: {
    input: "$join",
    initialValue: [],
    in: {
      $concatArrays: [
        "$value",
        "$this.favorite"
      ]
    }
  }
}

Complete query

db.testA.aggregate([
  {
    $match: {
      $or: [
        {
          "message": {
            $regex: "he"
          }
        },
        {
          "field3": {
            $regex: "s"
          }
        }
      ]
    }
  },
  {
    $lookup: {
      from: "testB",
      localField: "_id",
      foreignField: "favorite",
      as: "join"
    }
  },
  {
    $addFields: {
      favorite: {
        $cond: [
          {
            $in: [
              "$_id",
              {
                $reduce: {
                  input: "$join",
                  initialValue: [],
                  in: {
                    $concatArrays: [
                      "$value",
                      "$this.favorite"
                    ]
                  }
                }
              }
            ]
          },
          true,
          false
        ]
      }
    }
  }
])

Sample Mongo Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文