6

I would like to retrieve the following information:

select names from database where names like 'Jon';

but for MongoDB in Java. Essentially, it should return all names that contain the word Jon in them, like Jonathan, Jong etc etc. I know that there is the $in operator in MongoDB, but how do I do the same in Java, using the Java driver? I've been trying to look for it everywhere but am getting nothing. I've tried: query = new BasicDBObject("names", new BasicDBObject("$in", "Jon"));, and query = new BasicDBObject("names", new BasicDBObject("$in", Jon));

But neither of them worked :( Please help!

4
  • It would help if you posted some code that you have tried. Commented Dec 4, 2012 at 21:41
  • @dadu: I did, but there's nothing for my specific query. I tried query = new BasicDBObject("names", new BasicDBObject("$in", "Jon"));, and query = new BasicDBObject("names", new BasicDBObject("$in", Jon)); But neither of them worked :( I did look up Morphia too, but I'm kinda crunching on time, and implementing that is kinda beyond my time frame, especially since I only need one single query. Commented Dec 4, 2012 at 21:42
  • @AaronKurtzhals: Please refer my comment now. I realised I hadn't posted my code. Commented Dec 4, 2012 at 21:42
  • 2
    That SQL query won't do what you say it does. To get everything where the name includes the string "Jon", you would use LIKE "%Jon%". To get only those which start with Jon, you would use LIKE "Jon%". The MongoDB $in operator is for searching in array fields. To search for string fragments, you can use $regex and regular expressions. Commented Dec 4, 2012 at 21:55

3 Answers 3

10

The MongoDB Java driver equivalent of that SELECT statement would be:

BasicDBObject fields = new BasicDBObject().append("name", 1); // SELECT name
BasicDBObject query = new BasicDBObject().append("name", "Jon"); // WHERE name = "Jon"
DBCursor results = yourCollection.find(query, fields); // FROM yourCollection

When you want to search for a part of a string, you can use the $regex operator:

query = new BasicDBObject("name", new BasicDBObject("$regex", "Jon"));

This will get you all objects where the name matches the regular expression Jon, which is everything which includes the string "Jon" anywhere.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that's what I was looking for, my query was more like pseudocode, rather than actual code :P Can you please edit your answer accordingly? Thanks! :)
I edited my answer and added the equivalent to a LIKE "%Jon%" query.
Please look at my new question: stackoverflow.com/questions/13732735/… and please see if you can lend your expertise in any way. Thanks! :)
1

Check the Mongo-Java official site, plus a nice library for mapping mongo objects

Comments

0

The $in operator in MongoDB resembles the IN operator in SQL. For instance, it can be used for executing SQL-like queries as

SELECT * FROM table WHERE id IN {1, 4, 6, 7, 9}

This is why it doesn't work for you. Use Regular expressions, as the others have already suggested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.