Skip to content

Instantly share code, notes, and snippets.

@deslee
Created June 16, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deslee/8a32036030c15c189edde1fd9b8cb449 to your computer and use it in GitHub Desktop.
Save deslee/8a32036030c15c189edde1fd9b8cb449 to your computer and use it in GitHub Desktop.
getRandomPun(): Promise<Pun> {
return this.client.db('punbot').collection('puns').aggregate([
{ '$sample': { 'size': 1 } }
]).toArray().then(result => {
if (result.length) {
return result[0];
}
return undefined;
}).catch(err => {
console.error(err);
});
}
getSpecificPun(query: string): Promise<Pun[]> {
return this.client.db('punbot').collection('puns')
.find(
{ '$text': { '$search': query } }
)
.project(
{ 'text': 1, 'score': { '$meta': 'textScore' } }
)
.sort({ 'score': { '$meta': 'textScore' } })
.toArray().then(result => {
return result;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment