ezeikel
02/06/2022, 12:12 AMfindMany
query and some
operator:
When running this query:
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:
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?ezeikel
02/06/2022, 12:18 AMclusters
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 🤔ezeikel
02/07/2022, 4:50 PMconst 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? 🤔ezeikel
02/08/2022, 1:39 PMConnectorError(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:
followedClusters Cluster[] @relation("ClusterFollowers")
to
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 followerezeikel
02/08/2022, 1:47 PMprisma.cluster.findMany
ezeikel
02/09/2022, 12:14 AMset
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