使用Mongoskin在每个产品的字段描述中通过单词查找MongoDB文档 [英] Finding a MongoDB document through a word in a field description in each product with Mongoskin

查看:136
本文介绍了使用Mongoskin在每个产品的字段描述中通过单词查找MongoDB文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在MongoDB中拥有的文档的示例:

This is an example of document that I have in my MongoDB:

{
    "_id": ObjectId('5525039895884d66710d0fc3'),
    "prid": "63527",
    "data": {
        "sku": "HF22-81639",
        "name": "Product Test",
        "ean": "8763900872512",
        "description": "This product is my first test",
    }
}

此搜索描述"无效(这是我需要帮助的地方):

This search for "description" does not work (this is where I need help):

app.get("/description/:id", auth, function(req, res, next) {
    req.collection.findOne({
        "data.description": req.params.id
    }, function(e, result) {
        if(e) return next(e);
        res.send(result);
    });
});

我需要通过一个单词来查找,集合中存在的所有产品都在该单词的描述字段中包含该单词.

I need to find through a word, all the products that exist in the collection include in the description field that word.

推荐答案

要通过单词查找,集合中存在的所有产品都在单词的描述字段中包含该单词,您需要一个不区分大小写的正则表达式匹配项.您可以使用以下查询(例如):

To find through a word, all the products that exist in the collection include in the description field that word, you need a regex match with case insensitivity. You could use the following query (as an example):

db.product.find({"data.description": /test/i});

,其中/test/i中的i表示不区分大小写,因此正则表达式在描述字段中匹配字符串"test"的任何文本.等效的SQL表达式如下:

where the i in the /test/i indicates case insensitivity, thus the regex matches on the description field for any text with the string "test". The equivalent SQL expression follows:

select * from product where description like '%test%'

因此,您可以使用 find() 方法返回所有匹配的文档,而不是

So you could use the same in your route implementation, using the find() method to return all matched documents instead of the findOne() which returns just one document:

app.get("/description/:id", auth, function(req, res, next) {
    req.collection.find({
        "data.description": /req.params.id/i
    }, function(e, result) {
        if(e) return next(e);
        res.send(result);
    });
});

另一种选择是使用 $text > 运算符,因为它会对使用文本索引建立索引的字段的内容执行文本搜索.因此,您要做的第一件事是在说明字段上创建一个文本索引:

Another option is to use the $text operator in your find operation as it performs a text search on the content of the fields indexed with a text index. So the first thing you would do is create a text index on the description field:

db.collection.createIndex( { "data.description": "text" } )

之后,您可以使用$ text运算符进行查询.例如,以下查询搜索术语咖啡:

After that you can query using the $text operator. For example, the following query searches for the term coffee:

db.collection.find( { $text: { $search: "coffee" } } )

编辑:

在所有条件都相同的情况下,您可以更新路由实现,以改用URL中的查询字符串:

All things being equal, you can then update your route implementation to use query strings in the URL instead:

app.get("/description", auth, function(req, res, next) {
    req.collection.find({
        $text: { $search: req.params.q }
    }, function(e, result) {
        if(e) return next(e);
        res.send(result);
    });
});

,您可以在浏览器中将其查询为http://localhost/description?q=product

which you can query in your browser as http://localhost/description?q=product

这篇关于使用Mongoskin在每个产品的字段描述中通过单词查找MongoDB文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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