简体   繁体   中英

How can i use regex in mongodb over mongolab?

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'});

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.

For example : i have a json:

[{"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 "}]

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.*' }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM