Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate keywords from existing documents #15

Open
derkweijers opened this issue Jan 5, 2019 · 3 comments
Open

Generate keywords from existing documents #15

derkweijers opened this issue Jan 5, 2019 · 3 comments
Labels

Comments

@derkweijers
Copy link

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!

@diegohaz
Copy link
Owner

diegohaz commented Jan 5, 2019

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

@derkweijers
Copy link
Author

Thanks, I'll take a look at it!

@nickhingston
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants