(Mongodb) 获取错误 (BadValue) $in needs an array , when using $map aggregate function

(Mongodb) Getting an error (BadValue) $in needs an array , when using $map aggregate function

我正在尝试使用 map 函数将 字符串数组 转换为 ObjectId 数组,map 将 return 我 ObjectId 数组 我在 $match 中使用它来检查 _id 是否与使用 $in 运算符的数组中存在的任何 objectid 匹配。

数据库配置为

 db={
  "students": [
    {
      "_id": ObjectId("5c1a79f7c98da061141475b7"),
      "firstName": "Ibrahim",
      "kelasID": ObjectId("5c429f9906f2a805bc6cd494"),
      "lastName": "Ali",
      "schoolID": ObjectId("5c1a735fc98da061141475a1"),
      "year": 2018,
      "__v": 0,
      "addedOn": ISODate("2018-12-25T04:27:47.909Z"),
      "checkIn": false,
      "checkInStatus": 1,
      "contactNo1": "012225656",
      "father": "Ali",
      "fatherID": "8852245",
      "idType": 0,
      "lastModified": ISODate("2018-12-25T04:27:47.909Z"),
      "mother": "",
      "motherID": ""
    },
    {
      "_id": ObjectId("5c3bfea37774fb0b55000cb5"),
      "idType": 0,
      "checkIn": false,
      "checkInStatus": 1,
      "year": 2019,
      "schoolID": ObjectId("5c1a735fc98da061141475a1"),
      "kelasID": ObjectId("5c1a7534c98da061141475a3"),
      "firstName": "Umar",
      "lastName": "Bin Al-Khattab",
      "contactNo1": "601222",
      "status": 1,
      "addedOn": ISODate("2019-01-14T03:14:43.597Z"),
      "lastModified": ISODate("2019-01-14T03:14:43.597Z"),
      "__v": 0
    },
    {
      "_id": ObjectId("5c1a7c69c98da061141475bb"),
      "idType": 0,
      "checkIn": false,
      "checkInStatus": 1,
      "year": 2018,
      "schoolID": ObjectId("5c1a735fc98da061141475a1"),
      "kelasID": ObjectId("5c1a7540c98da061141475a5"),
      "firstName": "Abdul Rahman",
      "lastName": "Affan",
      "father": "Affan",
      "fatherID": "54321",
      "contactNo1": "602288",
      "status": 1,
      "addedOn": ISODate("2018-12-25T04:30:16.130Z"),
      "lastModified": ISODate("2018-12-25T04:30:16.130Z"),
      "__v": 0
    }
  ]
}

查询是

db.students.aggregate([
  {
    "$match": {
      "_id": {
        $in: {
          "stud": {
            "$map": {
              "input": [
                "5c1a79f7c98da061141475b7",
                "5c3bfea37774fb0b55000cb5",
                "5c1a7c69c98da061141475bb",
                "5c3bfea37774fb0b55000cb4",
                "5c1a7d32c98da061141475be",
                "5c3bfea37774fb0b55000cb7"
              ],
              "in": {
                "$toObjectId": "$$this"
              }
            }
          }
        }
      }
    }
  }
])

您也可以在这里查看 - https://mongoplayground.net/p/8tQFHxtrttW 上面的方法不起作用,但如果我像下面这样更改查询,它就会起作用。

db.students.aggregate([
  {
    "$match": {
      "_id": {
        $in: [
          ObjectId("5c1a79f7c98da061141475b7"),
          ObjectId("5c3bfea37774fb0b55000cb5")
        ]
      }
    }
  }
])

Link 到上面的查询- https://mongoplayground.net/p/cjq77KIAcPE

我收到错误 - 查询失败:(BadValue) $in 需要一个数组 任何人都可以帮助解决错误的原因,因为根据 docs map return 一个数组,所以我做错了什么?

不确定为什么它不起作用可能与 $in operator and $in aggregation. When you use an array that is defined by a variable you can use $in operator but when you are using aggregation result ( in this case $map ) you have to use $in aggregation. But to use $in aggregation 有关,您需要使用 $expr 来获得 $match。

您可以试试下面的代码,效果很好

db.students.aggregate([
      {
        $addFields: {
          studentIds: {
            $map: {
              input: [
                "5c1a79f7c98da061141475b7",
                "5c3bfea37774fb0b55000cb5",
                "5c1a7c69c98da061141475bb",
                "5c3bfea37774fb0b55000cb4",
                "5c1a7d32c98da061141475be",
                "5c3bfea37774fb0b55000cb7",
              ],
              in: {
                $toObjectId: "$$this",
              },
            },
          },
        },
      },
    
      {
        $match: {
          $expr: {
            $in: ["$_id", "$studentIds"],
          },
        },
      },
   ]);

或者如果你想结合这两个阶段,你可以这样做

db.students.aggregate([
{
    $match: {
      $expr: {
        $in: [
          "$_id",
          {
            $map: {
              input: [
                "5c1a79f7c98da061141475b7",
                "5c3bfea37774fb0b55000cb5",
                "5c1a7c69c98da061141475bb",
                "5c3bfea37774fb0b55000cb4",
                "5c1a7d32c98da061141475be",
                "5c3bfea37774fb0b55000cb7",
              ],
              in: {
                $toObjectId: "$$this",
              },
            },
          },
        ],
      },
    },
  },
]);