简体   繁体   English

使用 Nodejs Mongoose 自动完成/建议搜索社交聊天应用程序

[英]Autocomplete/suggest search for a social chat app with Nodejs Mongoose

I'm trying to come with the most practical search possible for an auto suggest search bar like the one on instagrams (it's able to match regex of strangers).我正在尝试为自动建议搜索栏提供最实用的搜索,例如 instagram 上的搜索栏(它能够匹配陌生人的正则表达式)。 So let's say I have a million user with their own unique handle.因此,假设我有一百万用户拥有自己独特的句柄。 I want to be able to do the suggest search as the user is typing on the front-end.我希望能够在用户在前端输入时进行建议搜索。 For ex if the current strings of the user type field are 'dav' its will match any unique handles in the entire Mongodb for that regex.例如,如果用户类型字段的当前字符串是“dav”,它将匹配该正则表达式的整个 Mongodb 中的任何唯一句柄。 I know mongo does support find field with regex but how do I limit the maximum of results for efficiency.我知道 mongo 确实支持使用正则表达式查找字段,但我如何限制最大结果以提高效率。 I cannot find an answer on this for Mongoose.我找不到 Mongoose 的答案。 Say the user is search for 'dave' and there's like 3k results that match 'dave' but it will stop at search at the first 10 and return those result.假设用户正在搜索“dave”,并且有 3k 个结果与“dave”匹配,但它会在前 10 个搜索停止并返回这些结果。 So the user gonna have to be more specific with the unique handle.所以用户必须更具体地使用独特的句柄。 Thank you.谢谢你。

You can do that using limit() (and additionally skip() if you want to use pagination)您可以使用 limit() 来做到这一点(如果您想使用分页,还可以使用 skip())

db.users.find(...).limit(10)

This will return the first 10 results.这将返回前 10 个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM