首页 > 解决方案 > How to implement common search in mongo id when using dynamic attributes in Rails

问题描述

I have a collection called Example, all the fields of a collection are dynamic. I want to perform a common search. I have no idea how I can do. For example, if I search for "hello" it should return all the objects of a collection which contains name irrespective of the fields the value contained.

Example:

{ id: 111234, desc: 'hello world' },     
{ id: 545ttt, title: 'hello title' }, 
{ id: sfsd3, data: 'hello' }

My query should return all the above records if I search for hello. I found a mongoid_search gem but I did not find anything related to dynamic fields search.

Thanks in Advance.

标签: ruby-on-railsmongodbmongoid

解决方案


步骤 1.在所有字段上创建文本索引。

db.collection.createIndex({ "$**": "text" },{ name: "TextIndex" })

第 2 步。您可以使用$text作为以下查询对象进行简单的文本搜索:

{ $text : { $search: <your string> } }

推荐阅读