How can we do advanced querying on nested arrays ??
I've created this schema for one of my documents
export const DocumentColumns = {
title: v.string(),
userId: v.string(),
content:v.string(),
collaborators: v.optional(
v.array(
v.object({
name: v.string(),
id: v.string(),
imgUrl: v.string(),
})
)
),
};
How can we query all the documents by matching some of the filed values of collaborator field in the collaborators array ??
Just like we do in mongoDb like this!!
const documents = await Documents.find({ "$documents.name": "any" }).exec()
Or if this is not possible than how can we acheive the same using convex queries? do we need to fetch all the documents from our DB at first and then manually do filtering of documents?
Appreciate any help here!
cc: @ballingt @Michal Srb
4 Replies
You do not need to fetch all the document, but that is what you're forced to do if you use this schema. Instead we'd recommend using a relations here. Here are some good resources: https://www.convex.dev/can-do/relational-data
if you want to do the thing that's equivalent to mongodb, and very slow (just as slow as mongo would be 🙂 ), there's a helper https://stack.convex.dev/complex-filters-in-convex
Using TypeScript to Write Complex Query Filters
There’s a new Convex helper to perform generic TypeScript filters, with the same performance as built-in Convex filters, and unlimited potential.
Hey thanks think the same before but bit confused about relations in convex but thanks I'll study those articles and try implementing relations to get this resolved!
hahahaha