我怎样才能在mongodb中使用mongodb中的正则表达式?

时间:2014-10-05 10:55:42

标签: regex json node.js mongodb

我的问题很简单但我无法找到确切的解决方案。所有文章都有一行代码:

collection.findOne({你好: 'world_no_safe'});

以上代码确实有效,并返回给我未定义的错误。但它存在。无论如何,我的确切解决方案在上面。请帮助我,通过搜索json api来教我如何使用正则表达式。我希望使用%mysearch数据%作为正则表达式。 ı想要在json中使用所有句子的文本搜索。

例如:我有一个json:

[{“data”:“Andreas Kollegger解释了你可以用图形数据库回答哪些问题。不仅是大型网站”},{“数据”:“X解释了你可以回答的问题类型使用图表数据库。不仅是“}]

的大型网站

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


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

});

1 个答案:

答案 0 :(得分:2)

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

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