优化-在Mongoose MongoDB模式中的所有字段上查找 [英] Optimization - find on all fields in Mongoose MongoDB schema

查看:77
本文介绍了优化-在Mongoose MongoDB模式中的所有字段上查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要挑战我们的数据库方法,需要您的帮助

We needed to challenge our database approach and need your help

我们需要在Mongoose模式的所有字段中搜索单词/词组.

We needed to search a word/phrase in all fields of a Mongoose schema.

让我们说模式是这样的:

Let's say the schema is like this:

var sampleSchema = new Schema({
    fieldABC: String,
    fieldDEF: String,
    fieldGHI: String
});

我们需要编写一个查找查询,该查询将在集合文档的所有字段中搜索一个单词:

We need to write a find query which will search for a word in all fields in a document of the collection:

db.sampleCollection.find({
  $or: [{
    fieldABC: "wordToSearch"
  }, {
    fieldDEF: "wordToSearch"
  }, {
    fieldGHI: "wordToSearch"
  }]
})

我们可以编写上面的查询,但是看起来效率很低-有更好,更快的方法吗?

It's possible for us to write the above query but it looks very inefficient - is there some better and faster approach to this?

在2015年,它不受支持,这有什么变化吗?

推荐答案

作为 @Veeram

创建文本索引

db.sampleCollection.createIndex( { "$**": "text" } )

步骤2:

使用文本索引搜索相关单词

Step 2:

Use the text index to search the word in concern

db.sampleCollection.find( { $text: { $search: "wordToSearch" } })

这篇关于优化-在Mongoose MongoDB模式中的所有字段上查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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