猫鼬或查询 [英] Mongoose or query

查看:92
本文介绍了猫鼬或查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据多个文本字段查找文档.

I need to find documents based on multiple text fields.

var term = new RegExp(req.query.search, 'i');
.find({ company_name: { $regex: term }});

使用上述方法效果很好.但是,当我尝试使用

Using the above works great. However, when I try to add an additional field using

.find(...).or([{ bio: { $regex: term }}]);

它无法检索任何记录.我需要在其他几个字段中执行此操作.

it fails to retrieve any records. I need to do this for a few more fields.

该架构为不同的文本字段建立了索引,但没有将它们作为单个的多字段索引结合在一起.这样做会更好吗?如果是这样,是否有任何明确的例子?我发现的文档很少.

The schema has the different text fields indexed, but not together as a single multi-field index. Would this work better and if so, are there any clear examples of this? The documentation that I found was quite sparse.

有什么想法吗?

推荐答案

将两个字段都放入传递给

Put both fields into an array that's passed to or:

.find().or([{ company_name: { $regex: term }}, { bio: { $regex: term }}]);

单独的单字段索引是您想要的,因为每个or术语都作为单独的查询执行.

Separate single-field indexes are what you want as each or term is executed as a separate query.

这篇关于猫鼬或查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆