Hey all :wave::skin-tone-6: having an issue with a...
# orm-help
e
Hey all 👋🏿 having an issue with a
findMany
query and
some
operator: When running this query:
Copy code
const clusters = await prisma.cluster.findMany({
  where: {
    followers: { some: { id: userId } },
  },
});
I get this error:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (Location40081): $in requires an array as a second argument, found: objectId)" } })
The two related models look like this:
Copy code
model Cluster {
  id          String   @id @default(dbgenerated()) @map("_id") @db.ObjectId
  name        String
  description String?
  sparks      Spark[]  @relation(fields: [sparkIDs])
  sparkIDs    String[] @db.Array(ObjectId)
  user        User     @relation("ClusterOwner", fields: [ownerID], references: [id])
  ownerID     String   @db.ObjectId
  shared      Boolean  @default(false)
  deleted     Boolean  @default(false)
  followers   User[]   @relation("ClusterFollowers", fields: [followerIDs])
  followerIDs String[] @db.Array(ObjectId)
  legacyId    String?  @unique
  meta        Meta[]   @relation(fields: [metaIDs])
  metaIDs     String[] @db.Array(ObjectId)
  log         Log[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@unique([ownerID, id])
  @@map("clusters")
}

model User {
  id               String         @id @default(dbgenerated()) @map("_id") @db.ObjectId
  firstName        String?
  lastName         String?
  name             String?
  email            String         @unique // TODO: doesnt seem to be being enforced
  type             UserType       @default(EXTERNAL_SEA_USER)
  pin              String         @default("0000")
  role             UserRole       @default(USER)
  deleted          Boolean        @default(false)
  legacyId         String?        @unique
  clusterFollowers Cluster[]      @relation("ClusterFollowers")
  clusterOwner     Cluster[]      @relation("ClusterOwner")
  processedForUser Log[]          @relation("ProcessedForUser")
  processedByUser  Log[]          @relation("ProcessedByUser")
  sparks           Spark[]
  subscriptions    Subscription[]
  createdAt        DateTime       @default(now())
  updatedAt        DateTime       @updatedAt

  @@map("users")
}
Any ideas on what I might be doing wrong or is this a bug with the mongo connector?
Also, FYI this query runs fine if there are zero
clusters
in the database but once one has been created I get this error. Not sure what the underlying query would in mongo for the prisma
some
operator but seems weird for it to be complaining about the wrong type of argument for the mongo
$in
operator 🤔
Weirdly this does work:
Copy code
const clusters = await prisma.cluster.findMany({
  where: {
    followerIDs: {
      has: userId,
    },
  },
});
This fixes the issue that was blocking me but I'm still wondering why I can't just use
some
with
followers
instead? 🤔
I figured out that the initial error message:
Copy code
ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (Location40081): $in requires an array as a second argument, found: objectId)" } })
Was caused by me not mapping out the many to many relationship between
User
and
Cluster
in the schema correctly, so updating my
User
model from:
Copy code
followedClusters Cluster[]      @relation("ClusterFollowers")
to
Copy code
followedClusters   Cluster[]      @relation("ClusterFollowers", fields: [followedClusterIDs])
  followedClusterIDs String[]       @db.Array(ObjectId)
got rid of the error but now, that query just returns an empty array even though I can see that it should match some documents 🤔 I have double checked this in MongoDB Compass using the query:
{ followerIDs: ObjectId('62022eef14826594a773bd45') }
Also, the above earlier solution to filter on
followerIDs
instead of
followers
only works as long as there is only one
user
following
cluster
if
followerIDs
array contains more than one element then the query returns an empty array So kind of back to square one in that the query that should work returns an empty array and the fix I thought I had found only works for a cluster with a single follower
I get the correct documents in MongoDB Compass but an empty array when using
prisma.cluster.findMany
In the process of creating a small reproduction repo for the GitHub issue for this I discovered what the problem was - when calling
set
on
cluster.followers
I was just passing an array of IDs and not
[{ id: 'id'}, ...]
which then messed up the relation when trying to filter on it 🤕 Everything is working fine now