Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mongodb for sql中的查询语法;例如我正在使用java_Java_Mongodb - Fatal编程技术网

mongodb for sql中的查询语法;例如我正在使用java

mongodb for sql中的查询语法;例如我正在使用java,java,mongodb,Java,Mongodb,基本上这是我的问题: 但是我想这里的所有答案在使用mongodb shell命令行时都是适用的,那么当我们使用java时,db.users.find({“name”:/*m.*/})的等价物是什么呢 下面是我试图做的 对于此问题,应使用正则表达式,如本例所示: BasicDBObject query = new BasicDBObject(); Pattern regex = Pattern.compile("the title you are looking for"); // should

基本上这是我的问题:

但是我想这里的所有答案在使用mongodb shell命令行时都是适用的,那么当我们使用java时,
db.users.find({“name”:/*m.*/})
的等价物是什么呢

下面是我试图做的


对于此问题,应使用正则表达式,如本例所示:

BasicDBObject query = new BasicDBObject();
Pattern regex = Pattern.compile("the title you are looking for"); // should be m in your case
query.put("title", regex);

以上述示例为参考,但使用Filters类:

Pattern regex = Pattern.compile("title you look for");
Bson filter = Filters.eq("nombre", regex));

thnx。你的意思是我应该写:Pattern regex=Pattern.compile(“/.*m.*/”);如果我想找到包含“m”的标题@dave,您只需编写Pattern regex=Pattern.compile(“m”)+1-请注意,MongoDB将“做正确的事情”,并在可能的情况下使用正则表达式执行有效的搜索(基本上是在正则表达式末尾只有一个通配符时),使用模式使搜索变得非常简单!我是否可以使用模式而不是类似的东西:query.put(“price”,newBasicDBObject(“$gt”,5)。append($lt”,8));我越来越渴望得到更多的安慰:D@dave:可能不是:-(。即使您描述的查询确实有效,MongoDB也无法再使用索引来解析它,因此可能会使查询速度慢得多。在我看来,最好将正则表达式的使用限制在SQL查询中使用LIKE子句的位置。
BasicDBObject query = new BasicDBObject();
Pattern regex = Pattern.compile("the title you are looking for"); // should be m in your case
query.put("title", regex);
Pattern regex = Pattern.compile("title you look for");
Bson filter = Filters.eq("nombre", regex));