mongoose-keywords
mongoose-keywords copied to clipboard
Generate keywords from existing documents
Hi!
I'm rebuilding my API to one generated with generator-rest (great work BTW), but I'm facing an issue. I noticed that querying the API only works for documents posted to the endpoint generated by generator-rest. I suppose it calls mongoose-keywords to generate the keywords, so it will only find those.
Is there a way to generate the keywords for the 1800 documents I already have in my database, that were posted prior to using generator-rest?
Thanks in advance!
The only solution that comes to my mind is performing a migration on your database data, generating keywords for the existing documents. You can probably use something like https://github.com/tj/node-migrate
Thanks, I'll take a look at it!
For anyone else, I solved this problem using https://github.com/balmasi/migrate-mongoose
and adding this to the up method:
async function up () {
const cursor = MyModel.find().cursor()
// same transform as mongoose-keywords
const transform = (value) => _.kebabCase(value).replace(/-/g, ' ')
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
doc.keywords.addToSet(transform(doc.fieldIWannaSearchOn))
doc.keywords.addToSet(transform(doc.someOtherField))
await doc.save()
console.log(doc.id)
}
}```