1

How do I write this query on java api ? ',' means 'or' clause

db.questions.find( { user_id: /bc/ },{user_name:/bc/},{age:/2/} )

I've tried different ways, but I've been successful using one option only. How to add multiple options using "or" clause ?

here my code :

MongoClient mongoClient;
                DB db;

                DBCollection table;
                DBCursor cursor = null;

                mongoClient = new MongoClient("localhost", 27017);
                db = mongoClient.getDB("stackoverflow");
                boolean auth = db.authenticate("aku", "kamu".toCharArray());
                table = db.getCollection("questions");
                cursor = table.find(new BasicDBObject("user_id", java.util.regex.Pattern.compile("milk")));
                while (cursor.hasNext()) {
                    DBObject object = cursor.next();%>
                    out.print(object.get("user_id"));
                }

Using mySQL, the query is :

select * from questions where user_id like '%bc%' or user_name like %bc% or age like %2%

I'm new to mongoDB, I feel like it's fairly hard to convert queries from mySQL..

here the error enter image description here

2 Answers 2

2

You can use the $or operator - manual link

db.questions.find( "{ $or : [user_id: '/bc/' ,user_name:'/bc/',age:'/2/'] }")

You can use this query within java like this:

FindIterable<Document> iterable = db.getCollection("questions").find( "{ $or : [user_id: '/bc/' ,user_name:'/bc/',age:'/2/'] }" );

iterable.forEach(new Block<Document>() {
    @Override
    public void apply(final Document document) {
        System.out.println(document);
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

anything else you need mate?
hey bro, how to apply it on web project (jsp)..it cant be implemented btw thanks, i just knowing thats way, so the query can be directly writen on java api like that...
i would recommend you to check a tutorial on this matter.
sure bro, here the picture of error i upload on the questions, can you make it clear bro?
dont forget " around the find. i will edit my post aswell
|
0

I'm not sure if your question is already answered but if you're using a later version or mongodb java driver, it can be written this way. I'm not familiar with how BasicDBObject works, am more used to Documents. Both works.

List<Document> orQueryList = new ArrayList<Document>();
orQueryList.add("user_id", "bc");
orQueryList.add("user_name", "bc");
orQueryList.add("age", "2");

Document orDoc = new Document("$or", orQueryList);

MongoCursor<Document> cursor = table.find(orDoc).iterator();

while (cursor.hasNext()) {
   Document dbObj = cursor.next();

   System.out.println(dbObj.getString("user_id");
}

2 Comments

thanks for helping @tan kim loong, actually my questions is not answered yet, i use mongoDB api version 2.X, i'll try your answer to implement my source...
It should mean that your "user_id" field is an Integer. Use dbObj.getInteger("user_id") or Integer.parseInt(dbObj.get("user_Id").toString()) instead.

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.