Fre*_*ind 1331 sql mongodb mongodb-query sql-like
我想查询一下SQL的like
查询:
SELECT * FROM users WHERE name LIKE '%m%'
Run Code Online (Sandbox Code Playgroud)
如何在MongoDB中做同样的事情?
我like
在文档中找不到运算符.
小智 1819
那必须是:
db.users.find({"name": /.*m.*/})
Run Code Online (Sandbox Code Playgroud)
或者,类似的:
db.users.find({"name": /m/})
Run Code Online (Sandbox Code Playgroud)
你正在寻找某处包含"m"的东西(SQL的' %
'运算符等同于Regexp' .*
'),而不是将"m"锚定到字符串开头的东西.
小智 340
db.users.insert({name: 'paulo'})
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})
db.users.find({name: /a/}) //like '%a%'
Run Code Online (Sandbox Code Playgroud)
出:paulo,patric
db.users.find({name: /^pa/}) //like 'pa%'
Run Code Online (Sandbox Code Playgroud)
出:paulo,patric
db.users.find({name: /ro$/}) //like '%ro'
Run Code Online (Sandbox Code Playgroud)
出:佩德罗
Afs*_*ani 261
在
你可以做:
db.users.find({'name': {'$regex': 'sometext'}})
Run Code Online (Sandbox Code Playgroud)
leo*_*eon 84
在PHP中,您可以使用以下代码:
$collection->find(array('name'=> array('$regex' => 'm'));
Run Code Online (Sandbox Code Playgroud)
Som*_*luk 44
已经有很多答案了.我正在为正则表达式的字符串搜索提供不同类型的要求和解决方案.
您可以使用包含单词的正则表达式,例如.您也可以使用$options => i
不区分大小写的搜索
包含 string
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
Run Code Online (Sandbox Code Playgroud)
不包含string
仅使用正则表达式
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
Run Code Online (Sandbox Code Playgroud)
确切的不区分大小写 string
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
Run Code Online (Sandbox Code Playgroud)
从...开始 string
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
Run Code Online (Sandbox Code Playgroud)
结束 string
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
Run Code Online (Sandbox Code Playgroud)
保持此为书签,以及您可能需要的任何其他改变的参考.
use*_*907 31
你有2个选择:
db.users.find({"name": /string/})
Run Code Online (Sandbox Code Playgroud)
要么
db.users.find({"name": {"$regex": "string", "$options": "i"}})
Run Code Online (Sandbox Code Playgroud)
在第二个上你有更多选项,比如"i"在选项中查找使用不区分大小写.关于"字符串",您可以使用" .string. "(%string%)或"string.*"(字符串%)和".*string"(%字符串)等.您可以使用正则表达式如你所愿.
请享用!
Edd*_*ddy 29
如果使用node.js,它表示你可以这样写:
db.collection.find( { field: /acme.*corp/i } );
//or
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );
Run Code Online (Sandbox Code Playgroud)
另外,你可以这样写:
db.collection.find( { field: new RegExp('acme.*corp', 'i') } );
Run Code Online (Sandbox Code Playgroud)
The*_*nse 21
你已经得到了答案,但要将正则表达式与不区分大小写相匹配
您可以使用以下查询
db.users.find ({ "name" : /m/i } ).pretty()
Run Code Online (Sandbox Code Playgroud)
的i
在/m/i
表示不区分大小写和.pretty()
提供了一个更漂亮输出
Sah*_*mar 21
在 MongoDb 中,可以像使用MongoDb 引用运算符正则表达式(regex)一样使用。
对于同一个前任。
MySQL - SELECT * FROM users WHERE name LIKE '%m%'
MongoDb
1) db.users.find({ "name": { "$regex": "m", "$options": "i" } })
2) db.users.find({ "name": { $regex: new RegExp("m", 'i') } })
3) db.users.find({ "name": { $regex:/m/i } })
4) db.users.find({ "name": /mail/ })
5) db.users.find({ "name": /.*m.*/ })
MySQL - SELECT * FROM users WHERE name LIKE 'm%'
MongoDb Any of Above with /^String/
6) db.users.find({ "name": /^m/ })
MySQL - SELECT * FROM users WHERE name LIKE '%m'
MongoDb Any of Above with /String$/
7) db.users.find({ "name": /m$/ })
Run Code Online (Sandbox Code Playgroud)
Aqi*_*taz 14
对于Node.js中的Mongoose
db.users.find({'name': {'$regex': '.*sometext.*'}})
Run Code Online (Sandbox Code Playgroud)
cma*_*o01 11
您可以使用2.6 mongodb的新功能:
db.foo.insert({desc: "This is a string with text"});
db.foo.insert({desc:"This is a another string with Text"});
db.foo.ensureIndex({"desc":"text"});
db.foo.find({
$text:{
$search:"text"
}
});
Run Code Online (Sandbox Code Playgroud)
Dap*_*Dap 11
对于PHP mongo喜欢.
我有几个与php mongo有关的问题.我发现连接正则表达式params有助于在某些情况下PHP mongo find字段开头.我想我会发布在这里为更受欢迎的线程做出贡献
例如
db()->users->insert(['name' => 'john']);
db()->users->insert(['name' => 'joe']);
db()->users->insert(['name' => 'jason']);
// starts with
$like_var = 'jo';
$prefix = '/^';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
output: (joe, john)
// contains
$like_var = 'j';
$prefix = '/';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
output: (joe, john, jason)
Run Code Online (Sandbox Code Playgroud)
Sha*_*Roy 10
在nodejs项目中并使用mongoose使用Like查询
var User = mongoose.model('User');
var searchQuery={};
searchQuery.email = req.query.email;
searchQuery.name = {$regex: req.query.name, $options: 'i'};
User.find(searchQuery, function(error, user) {
if(error || user === null) {
return res.status(500).send(error);
}
return res.status(200).send(user);
});
Run Code Online (Sandbox Code Playgroud)
dam*_*amd 10
使用MongoDB Compass,您需要使用严格模式语法,如下所示:
{ "text": { "$regex": "^Foo.*", "$options": "i" } }
Run Code Online (Sandbox Code Playgroud)
(在MongoDB Compass中,重要的是你使用"
而不是'
)
在SQL中,' like '查询如下所示:
select * from users where name like '%m%'
Run Code Online (Sandbox Code Playgroud)
在MongoDB控制台中,它看起来像这样:
db.users.find({"name": /m/}) // Not JSON formatted
db.users.find({"name": /m/}).pretty() // JSON formatted
Run Code Online (Sandbox Code Playgroud)
在addion pretty()
方法中,将生成格式化JSON结构的所有地方都更具可读性.
正则表达式是昂贵的过程.
另一种方法是创建文本索引,然后使用它进行搜索$search
.
创建要搜索的字段的文本索引:
db.collection.createIndex({name: 'text', otherField: 'text'});
Run Code Online (Sandbox Code Playgroud)
在文本索引中搜索字符串:
db.collection.find({
'$text'=>{'$search': "The string"}
})
Run Code Online (Sandbox Code Playgroud)
您可以使用where语句来构建任何JS脚本:
db.myCollection.find( { $where: "this.name.toLowerCase().indexOf('m') >= 0" } );
Run Code Online (Sandbox Code Playgroud)
参考:http://docs.mongodb.org/manual/reference/operator/where/
小智 7
在Go和mgo驱动程序中:
Collection.Find(bson.M{"name": bson.RegEx{"m", ""}}).All(&result)
Run Code Online (Sandbox Code Playgroud)
其中result是搜索类型的struct实例
使用如下匹配的正则表达式.'i'表示不区分大小写.
var collections = mongoDatabase.GetCollection("Abcd");
var queryA = Query.And(
Query.Matches("strName", new BsonRegularExpression("ABCD", "i")),
Query.Matches("strVal", new BsonRegularExpression("4121", "i")));
var queryB = Query.Or(
Query.Matches("strName", new BsonRegularExpression("ABCD","i")),
Query.Matches("strVal", new BsonRegularExpression("33156", "i")));
var getA = collections.Find(queryA);
var getB = collections.Find(queryB);
Run Code Online (Sandbox Code Playgroud)
如果你使用的是 Spring-Data MongoDB,你可以这样做:
String tagName = "m";
Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));
Run Code Online (Sandbox Code Playgroud)
像Query一样如下所示
db.movies.find({title: /.*Twelve Monkeys.*/}).sort({regularizedCorRelation : 1}).limit(10);
Run Code Online (Sandbox Code Playgroud)
对于scala ReactiveMongo api,
val query = BSONDocument("title" -> BSONRegex(".*"+name+".*", "")) //like
val sortQ = BSONDocument("regularizedCorRelation" -> BSONInteger(1))
val cursor = collection.find(query).sort(sortQ).options(QueryOpts().batchSize(10)).cursor[BSONDocument]
Run Code Online (Sandbox Code Playgroud)
似乎有理由同时使用javascript /regex_pattern/
模式和mongo {'$regex': 'regex_pattern'}
模式。请参阅:MongoBD RegEx语法限制
这不是完整的RegEx教程,但是在看到上面的一票vote昧的帖子后,我受到启发去运行这些测试。
> ['abbbb','bbabb','bbbba'].forEach(function(v){db.test_collection.insert({val: v})})
> db.test_collection.find({val: /a/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }
> db.test_collection.find({val: /.*a.*/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }
> db.test_collection.find({val: /.+a.+/})
{ "val" : "bbabb" }
> db.test_collection.find({val: /^a/})
{ "val" : "abbbb" }
> db.test_collection.find({val: /a$/})
{ "val" : "bbbba" }
> db.test_collection.find({val: {'$regex': 'a$'}})
{ "val" : "bbbba" }
Run Code Online (Sandbox Code Playgroud)
String yourdb={deepakparmar, dipak, parmar}
db.getCollection('yourdb').find({"name":/^dee/})
Run Code Online (Sandbox Code Playgroud)
ans deepakparmar
db.getCollection('yourdb').find({"name":/d/})
Run Code Online (Sandbox Code Playgroud)
ans deepakparmar, 迪帕克
db.getCollection('yourdb').find({"name":/mar$/})
Run Code Online (Sandbox Code Playgroud)
ans deepakparmar, 帕尔马
如果您有一个字符串变量,则必须将其转换为正则表达式,因此 MongoDB 将在其上使用 like 语句。
const name = req.query.title; //John
db.users.find({ "name": new Regex(name) });
Run Code Online (Sandbox Code Playgroud)
与以下结果相同:
db.users.find({"name": /John/})
Run Code Online (Sandbox Code Playgroud)
小智 5
查找与类似查询等效的结果的一种方法:
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
Run Code Online (Sandbox Code Playgroud)
wherei
用于不区分大小写的获取数据。
我们也可以通过另一种方式得到结果:
db.collection.find({"name":/aus/})
Run Code Online (Sandbox Code Playgroud)
上面将提供名称中包含aus的结果。