我如何通过mongolab在mongodb中使用正则表达式?

编程入门 行业动态 更新时间:2024-10-21 15:55:24
本文介绍了我如何通过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', 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中使用正则表达式?

本文发布于:2023-11-22 13:25:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   mongolab   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!