I am trying to add the keywords of a user’s username for faster querying using websocket events.
In my userSchema I have keywords index set to true:
keywords: {
// for live search of username
type: [String],
index: true,
},
For example if a user’s username is ‘johnsmith’, I need to save an array with the following..
user.keywords = [j, jo, joh, john, johns, johnsm, johnsmi, johnsmit, johnsmith]
I will be doing this in my userSchema.pre(‘save’).
Below is what I am going for but how would I append the username to the parts array and then set user.keywords = parts?? Am I properly indexing the user.keywords property??
Thank you!
userSchema.pre(`save`, async function (next) {
const user = this
if (user.isModified(`username`)) {
let parts = []
for (let i = 0; i < user.username.length; i++) {
parts.push(*???*)
}
user.keywords = parts
}
next()
})