我如何通过mongolab在mongodb中使用正则表达式? [英] How can i use regex in mongodb over mongolab?

查看:72
本文介绍了我如何通过mongolab在mongodb中使用正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,但我找不到确切的解决方案.所有文章的代码下面都包含gor:

My question is simple but i can not find exact solution. All articles have gor below a line of code:

collection.findOne({hello:'world_no_safe'});

collection.findOne({hello:'world_no_safe'});

上面的代码确实起作用并且返回给我未定义的错误.但是它存在.无论如何,以上是我的确切解决方案.请帮助我,并通过在json api中进行搜索来教我如何使用正则表达式.我想使用%mysearch data%作为正则表达式.我想进行文本搜索,该搜索将使用json中的所有句子.

Above codes does work and returns to me undefined error.But it is existed. Anyway, My exact solution is above. Please help me and teach me how to use regex by searching inside of the json api. ı want to use %mysearch data% as a regex expression. ı want to make text search which is used all sentences in json.

例如:我有一个json:

For example : i have a json:

[{"data":"Andreas Kollegger解释了您可以用图形数据库回答的问题.不仅仅是大型站点,"},{"data":"X解释了可以回答的问题.图形数据库.不仅仅是大型网站}]

[{"data":"Andreas Kollegger explains what kinds of questions you can answer with a graph database. It's not only the big sites that "},{"data":"X explains what kinds of questions you can answer with a graph database. It's not only the big sites that "}]

如果我使用以下表达式:collection.findOne({hello:'%Andreas%'}); 它必须返回第一个数据.下面是我的项目的真实示例.

if i use this expression: collection.findOne({hello:'%Andreas%'}); it has to return first data. Below a real sample from my project.


var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;


var server = new Server('ds053479.mongolab.com', 53479, {auto_reconnect : true});
var db = new Db('orient', server);


db.open(function(err, client) {
    client.authenticate('testname', 'fsfsff', function(err, success) {
        var collection = db.collection('Models');
           collection.insert({Name:'test1'});
       // Fetch the document
    collection.findOne({Name:'%world_no_safe%'});

});

推荐答案

根据 MongoDB手册 ,您可以使用$regex运算符:

collection.findOne({Name: { $regex: '.*world_no_safe.*' }});

这篇关于我如何通过mongolab在mongodb中使用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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