Do I have to create an index on the _id parameter?

Do I have to do this or is this automatically done by Convex? I want to find a value in my 200.000 document long db by id and I think to do this fast I need an index.
3 Replies
Abhishek
Abhishek•13mo ago
no need to create index on _id you can directly use db.get(pass the id)
FleetAdmiralJakob 🗕 🗗 🗙
Hi, thank you Abhishek, that worked. Now I need to parse the string on the frontend to ensure that is it an id:
const cityById = useQuery(
convexApi.getCity.findCityById,
typeof query.cityId === "string" ? { id: query.cityId } : "skip",
);`
const cityById = useQuery(
convexApi.getCity.findCityById,
typeof query.cityId === "string" ? { id: query.cityId } : "skip",
);`
TS2345: Argument of type "skip" | { id: string; } is not assignable to parameter of type { id: Id<"search">; } | "skip" How can I do that?
lee
lee•13mo ago
You can cast it query.cityId as Id<"search"> or you can pass it up as a v.string() and make sure it's an id in the query with const cityId = ctx.db.normalizeId("search", args.id)

Did you find this page helpful?