在 Mongodb 文档中查找 n 个单词 [英] Find n words inside a Mongodb document

查看:55
本文介绍了在 Mongodb 文档中查找 n 个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个查询来查找符合条件的文档集合,这是一个简单的搜索系统.问题是collection里面的assets是这样的:

Im trying to create a query that finds a collection of documents that match a criteria, a simple search system. The problem is that the assets inside the collection are like this:

{ 
 fristName: "foo",
 lastName: "bar",
 description: "mega foo",
},
{ 
 fristName: "Lorem",
 lastName: "Ipsum",
 description: "mega Lorem bla bla",
},

如果用户想要包含单词 bar 的所有资产,我应该显示示例的资产 1,这不是问题,但是如果他输入 foo mega?,在这种情况下,我还需要显示资产 1,因为 foomega 存在于资产 1(两者)中,如果他只搜索 mega,输出的是资产1和资产2,如果他搜索mega ipsum,结果是资产2.我不知道如何在mongoDb中编写这个查询.

If the user wants all the assets that contains the word bar i should show the asset 1 of the example, and that is not a problem, but what if he inputs foo mega?, in that case i need also to show the asset 1 because foo and mega are present in asset 1 (both of them), if he searchs only mega, the output is asset 1 and 2, and if he search mega ipsum, the result is asset 2. I have no idea how to write this query in mongoDb.

推荐答案

Mongodb 2.6+ 内置了对使用 $text 运算符的文本搜索的支持.这是使用方法.

Mongodb 2.6+ has built in support for text searches using the $text operator. Here's how to use it.

  1. 在所需的可搜索字段上建立文本索引.注意:对于 MongoDB 2.6,一个集合上只能有 1 个文本索引.

  1. Build an text index on the desired searchable fields. Note: For MongoDB 2.6 you can only have 1 text index on a collection.

在一个字段上创建文本索引

Create text index on one field

db.test.ensureIndex({ 
    "field1" : "text"
 }, { name : "Field1TextIndex"});

在两个字段上创建文本索引

Create text index on two fields

db.test.ensureIndex({ 
    "field1" : "text",
    "field2" : "text"
 }, { name : "Field12TextIndex"});

为任何字符串字段创建文本索引

Create text index for any string field

db.test.ensureIndex({ 
    "$**" : "text" 
}, { name : "AllTextIndex"});

  • 使用 $text 运算符查询集合.

  • Query the collection using the $text operator.

    这是$text

    { $text: { $search: <string of keywords>, $language: <string> } }
    

  • 示例代码

    设置

    use test;
    var createPerson = function(f,l,d){
        return { firstName : f, lastName: l, description : d};
    };
    db.test.remove({});
    db.test.insert(createPerson("Ben", "Dover", "Real Estate Agent"));
    db.test.insert(createPerson("James", "Bond", "secret agent, ben's friend"));
    

    在文档中的所有字符串字段上创建文本索引.

    db.test.ensureIndex({ "$**" : "text" }, { name : "AllTextIndex"});
    

    查询关键字的所有字段

    搜索ben

    db.test.find({  
        $text : {
            $search : "ben"
        }
    });
    

    输出

    { "_id" : "...", "firstName" : "James", "lastName" : "Bond", "description" : "secret agent, ben's friend" }
    { "_id" : "...", "firstName" : "Ben", "lastName" : "Dover", "description" : "Real Estate Agent" }
    

    搜索ben"会返回两个文档,因为一个文档的 Ben 作为 firstName,另一个文档的 ben's>描述字段.

    The search for "ben" returned both documents since one had Ben as the firstName, and the other had ben's in the description field.

    查询真正的朋友产生相同的结果.

    db.test.find({  
        $text : {
            $search : "real friend"
        }
    });
    

    更多信息在这里:

    这篇关于在 Mongodb 文档中查找 n 个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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