mongoose-keywords icon indicating copy to clipboard operation
mongoose-keywords copied to clipboard

Generate keywords from existing documents

Open derkweijers opened this issue 5 years ago • 3 comments

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!

derkweijers avatar Jan 05 '19 19:01 derkweijers

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

diegohaz avatar Jan 05 '19 20:01 diegohaz

Thanks, I'll take a look at it!

derkweijers avatar Jan 06 '19 14:01 derkweijers

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)
  }
}```

nickhingston avatar Oct 18 '20 07:10 nickhingston