Prisma MongoDB 在使用 $in

问题描述 投票:0回答:0

我有一个模式:

model Child {
    id        String   @id @default(auto()) @map("_id") @db.ObjectId
    parent    Parent @relation("children", fields: [id], references: [childrenIds])
}

model Parent {
    id           String    @id @default(auto()) @map("_id") @db.ObjectId
    childrenIds  String[]  @db.ObjectId @unique
    children     Child[]   @relation("children")
}

当我尝试获取关系时:

prisma.parent.findMany({
   include: {
      children: true
   }
})

children 数组总是空的。

好像数据库查询不正确:

db.child.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          {
            $in: [
              "$_id",
              [
                {
                  $literal: [
                    ObjectId(
                      "640639e3c9fb0d8a121d3839"
                    ),
                  ],
                },
                {
                  $literal: [
                    ObjectId(
                      "64063673c9fb0d8a121d30d4"
                    ),
                  ],
                },
              ],
            ],
          },
          {
            $ne: ["$_id", "$$REMOVE"],
          },
        ],
      },
    },
  },
  {
    $project: {
      _id: 1,
      name: 1,
    },
  },
])

我检查了 MongoDB Compass 中的查询,如果我像这样删除

$literal
包装器,它就会工作:

db.child.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          {
            $in: [
              "$_id",
              [
                ObjectId(
                  "640639e3c9fb0d8a121d3839"
                ),
                ObjectId(
                  "64063673c9fb0d8a121d30d4"
                ),
                ObjectId(
                  "64058246011ca90a5bf8b73e"
                ),
              ],
            ],
          },
          {
            $ne: ["$_id", "$$REMOVE"],
          },
        ],
      },
    },
  },
  {
    $project: {
      _id: 1,
      name: 1,
    },
  },
])

我的架构是否有问题导致 Prisma 添加该包装器?

mongodb orm prisma
© www.soinside.com 2019 - 2024. All rights reserved.