Mongo如何在多个字段中查询某个关键字?

发布时间:2017-7-1 11:21:47编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Mongo如何在多个字段中查询某个关键字?",主要涉及到Mongo如何在多个字段中查询某个关键字?方面的内容,对于Mongo如何在多个字段中查询某个关键字?感兴趣的同学可以参考一下。

If a text index has multiple fields in Mongo, how can I do a search in only one field since the weight is applied to the index and not to the query?

 

1 Answer

 

I assume you are using MongoDB version 2.4 to 3.x

You have no other choices than using MongoDB Text search.

below two approaches may helpful.

1. Indexing Multiple Fields (Compound Indexing)

db.yourcollection.createIndex({"textfield1":"text","textfield2":"text"})

Compound index is combination of both indices, works only if we search on both fields with given values.

2. Indexing the Entire Document (Wildcard Indexing)

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

Though the option two is not recommended. But if you use more text fields those are searchable every time, saves you time. One index fits for all.

reference: Full-Text Search in MongoDB (Just to take examples from it)

 

建立完索引后, 可以用如下的查询:

db.collectionName.find({$text: {$search: "keyword"}})

 

但是好像只能查到带有整个单词的记录,不能查到包含这个单词的记录。

 

详细的查询语法在这里: https://docs.mongodb.com/manual/reference/operator/query/text/


上一篇:抽象模型
下一篇:如何将HLS延时缩短至4秒,HLS+技术详解

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款