Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Syntax in mongodb for sql "like" - i am using java

Tags:

java

mongodb

basically this is my question: How to query MongoDB with "like"?

but i guess all the answers in here are applicable when you are using mongodb shell command line, so what is the equivalent for db.users.find({"name": /.*m.*/}) when we are using java.

here is how i am trying to do

  DBCollection coll = MongoDBUtil.getDB().getCollection("post_details");      
    BasicDBObject query = new BasicDBObject();      
    query.put("price", new BasicDBObject("$gt", 5).append("$lt", 8));
    /*what should i write instead of this line*/ 
            query.put("title", "/.*m.*/");

    DBCursor cur = coll.find(query);
like image 671
MoienGK Avatar asked May 09 '12 13:05

MoienGK


People also ask

Can MongoDB be queried with SQL?

In Data Virtuality, NoSQL databases such as MongoDB can be queried with standard SQL statements.

Which command in MongoDB is equivalent to SQL?

The concat function is a MongoDB string aggregation operators. Through mapping SQL functions to MongoDB operators, NoSQLBooster for MongoDB allows you to use all MongoDB aggregation operators as SQL functions in you SQL statement. No $ prefix with MongoDB operator and collection field name.


1 Answers

For this issue you should use a regex as in this example:

BasicDBObject query = new BasicDBObject();
Pattern regex = Pattern.compile("the title you are looking for"); // should be m in your case
query.put("title", regex);
like image 72
sebastian Avatar answered Oct 21 '22 14:10

sebastian