Bogdan
Bogdan16mo ago

Can i check equality like this?

the docs say to use combining operators if you want to for example return all documents where the name is either "Alex" or "Emma"
// Get all users named "Alex" or "Emma".
const usersNamedAlexOrEmma = await ctx.db
.query("users")
.filter((q) =>
q.or(q.eq(q.field("name"), "Alex"), q.eq(q.field("name"), "Emma"))
)
.collect();
// Get all users named "Alex" or "Emma".
const usersNamedAlexOrEmma = await ctx.db
.query("users")
.filter((q) =>
q.or(q.eq(q.field("name"), "Alex"), q.eq(q.field("name"), "Emma"))
)
.collect();
but i found that you could also do
const usersNamedAlexOrEmma = await ctx.db
.query("users")
.filter((q) =>
q.eq(q.field("name"), "Alex" || "Emma")
)
.collect();
const usersNamedAlexOrEmma = await ctx.db
.query("users")
.filter((q) =>
q.eq(q.field("name"), "Alex" || "Emma")
)
.collect();
is there a reason i should not use the ladder? it reads much better imo.
2 Replies
ballingt
ballingt16mo ago
The latter doesn't actually work — JavaScript execution rules turn it into
const usersNamedAlexOrEmma = await ctx.db
.query("users")
.filter((q) =>
q.eq(q.field("name"), "Alex")
)
.collect();
const usersNamedAlexOrEmma = await ctx.db
.query("users")
.filter((q) =>
q.eq(q.field("name"), "Alex")
)
.collect();
because "a" || "b" in JavaScript is just "a"
Bogdan
BogdanOP16mo ago
yeah you are absolutely right i just set up my tests wrong

Did you find this page helpful?