Skip to content

Instantly share code, notes, and snippets.

@ctrlaltdylan
Created September 11, 2020 00:24
Show Gist options
  • Save ctrlaltdylan/683d9897f9e18a6d1ceaef5ec4548f04 to your computer and use it in GitHub Desktop.
Save ctrlaltdylan/683d9897f9e18a6d1ceaef5ec4548f04 to your computer and use it in GitHub Desktop.
Mongod Text query with compound index
// Create the searchable checks index
db.collection("checks")
.createIndex({ shopName: 1, firstName: "text", lastName: "text" })
.then((res) => {
console.log("checks text index created");
})
.catch((err) => console.log("checks text index creation failed", err));
const checks = await db
.collection("checks")
.find({
shopName,
'$text': {
'$search': ctx.query.searchTerm
}
})
.sort({ createdAt: -1 })
.toArray();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment