简体   繁体   English

优化-在Mongoose MongoDB模式中的所有字段上查找

[英]Optimization - find on all fields in Mongoose MongoDB schema

We needed to challenge our database approach and need your help 我们需要挑战我们的数据库方法,需要您的帮助

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? 我们可以写上面的查询,但是看起来效率很低-是否有更好,更快的方法呢?

In the year 2015, it was not supported , is there any change in this? 在2015年,它不受支持 ,这有什么变化吗?

As suggested by @Veeram 正如@Veeram 建议那样

Step 1: 第1步:

Create a text index 创建文本索引

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

Step 2: 第2步:

Use the text index to search the word in concern 使用文本索引搜索相关单词

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

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

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