如何使用 Prisma 在 MongoDB 中查询对象

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

how to query object in mongo using prisma

问题

我想使用 Prisma 和 MongoDB 中的对象查询,请看我的 Prisma 架构:

model Member {
  id     String   @id @default(auto()) @map("_id") @db.ObjectId
  bank   BankInfo
  @@map("members")
}

type BankInfo {
  bankNo   String
}

在我的代码中,我尝试使用以下方式进行查询:

const member = await this.prisma.member.findFirst({
      where: { bank: { bankNo: 'test' } },
});

但是我遇到了一个错误:

错误信息:Invalid prisma.member.findFirst() invocation:

英文:

I want to query by using where in object with prisma and mongo
Here is my prisma schema

model Member {
  id                 String               @id @default(auto()) @map("_id") @db.ObjectId
  bank    BankInfo
  @@map("members")
}

type BankInfo {
  bankNo   String
}

 const member = await this.prisma.member.findFirst({
      where: { bank: { bankNo: 'test' } },
 });

I got an error
Error:
Invalid prisma.member.findFirst() invocation:

答案1

得分: 1

你可以尝试使用等于运算符($eq),如下所示:

const member = await this.db.member.find({
    bank: { $eq: { bankNo: 'test' } }
})
英文:

You can try to use equal operator ($eq) like that :

const member = await this.db.member.find({
    bank: { $eq: { bankNo: 'test' } }
})

huangapple
  • 本文由 发表于 2023年2月18日 19:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492963.html
匿名

发表评论

匿名网友

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

确定