【发布时间】:2016-05-03 15:36:52
【问题描述】:
您好,我正在为 mongoose 集合创建搜索选项。问题在于关键字处理。我需要处理关键字,例如它们是否在“”中,它们应该被视为单个单词,如果它们是逗号分隔或空格分隔,那么它们应该被视为 OR 条件参数,如这些我如何跟踪“”中的多个输入和逗号分隔。任何参考或示例都会非常有帮助。
【问题讨论】:
标签: node.js search express mongoose full-text-search
您好,我正在为 mongoose 集合创建搜索选项。问题在于关键字处理。我需要处理关键字,例如它们是否在“”中,它们应该被视为单个单词,如果它们是逗号分隔或空格分隔,那么它们应该被视为 OR 条件参数,如这些我如何跟踪“”中的多个输入和逗号分隔。任何参考或示例都会非常有帮助。
【问题讨论】:
标签: node.js search express mongoose full-text-search
查看$text 操作员文档。
假设您有一个名为 posts 的集合,其中包含以下文档:
{postText: 'The dog ran really fast.'},
{postText: 'The cat ran pretty slow.'},
{postText: 'That hampster is crazy fast.'}
确保您已创建文本索引。
db.posts.createIndex({postText: "text"})
对于您的 -OR- 情况,只需传入以空格分隔的关键字即可。
// this will return all documents
db.posts.find({$text: {$search: 'dog cat hampster'}})
// this will return two documents (dog & hampster)
db.posts.find({$text: {$search: 'really fast'}})
要准确匹配短语,请将其括在引号中。
// this will return one document (dog)
db.posts.find({$text: {$search: '"really fast"'}})
你也可以组合它们。
// this will return all documents
db.posts.find({$text: {$search: '"really fast" cat'}})
希望对您有所帮助。
【讨论】: