AND OR query in java

46 views
Skip to first unread message

Ronnie

unread,
Apr 26, 2012, 1:37:36 AM4/26/12
to mongodb-user
Hi,

Is there any java support for mongo db against the following sql
query?

Select x,y,z where
WHERE
( ( a = 'urn:ogc:def:phenomenon:OGC:1.0.30:test1 OR
a = 'urn:ogc:def:phenomenon:OGC:1.0.30:test2' OR
AND ((( s = 'TestArea')))
AND (( p = 'TestSquare)) AND
(( (t.time_stamp > '2011-09-01T17:44:15.000Z' AND t.time_stamp <
'2011-11-01T17:44:15.000Z') ))

It would be better if I get a java sample code base against the above
query sample.

Thanks in advance.

Andreas Jung

unread,
Apr 26, 2012, 1:43:30 AM4/26/12
to mongod...@googlegroups.com
What did you try so far?

> ( ( a = 'urn:ogc:def:phenomenon:OGC:1.0.30:test1 OR
> a = 'urn:ogc:def:phenomenon:OGC:1.0.30:test2' OR

can replace with an $in.

> (( (t.time_stamp > '2011-09-01T17:44:15.000Z' AND t.time_stamp <
> '2011-11-01T17:44:15.000Z') ))

is clearly a range search using $lt and $gt

and all queries combined using an implicit AND (which is the
default in MongoDB).

Putting this into Java code should not be that hard.

-aj
--
ZOPYX Limited | zopyx group
Charlottenstr. 37/1 | The full-service network for Zope & Plone
D-72070 T�bingen | Produce & Publish
www.zopyx.com | www.produce-and-publish.com
------------------------------------------------------------------------
E-Publishing, Python, Zope & Plone development, Consulting


Ronnie

unread,
Apr 26, 2012, 1:56:00 AM4/26/12
to mongodb-user
Regarding the date range query it is ok.
But i cannot get the OR , AND !

I am trying with AND.

eg :

colls = db.getCollectionNames();
query.put("p", "TestArea");
query.append("p", "TestSquare");
for (String s : colls) {
//System.out.println(s);
DBCollection coll = db.getCollection(s);
DBCursor cur = coll.find(query);

while(cur.hasNext()) {
//System.out.println(cur.next());
//System.out.println(cur);
DBObject obj = cur.next();
System.out.println(obj);

}
}

But this only returns me the results against p:TestSquare , but not
both.

Need some help , I am not getting the syntax through java.
> D-72070 T bingen        | Produce & Publishwww.zopyx.com          |www.produce-and-publish.com

H.J

unread,
Apr 26, 2012, 3:26:55 AM4/26/12
to mongod...@googlegroups.com
>                query.put("p", "TestArea");
>                query.append("p", "TestSquare");

why you use different methods here? put and append.

2012/4/26 Ronnie <anku...@tcs.com>:
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>



--
尽人事,听天命!

Ronnie

unread,
Apr 26, 2012, 4:00:41 AM4/26/12
to mongodb-user
I want to get all the data for bot TestArea and TestSquare.

What will be java code look like?
My problem is that I am not able to get the syntax in java for AND
operators which we get in sql.

Please provide some examples.

On Apr 26, 12:26 pm, "H.J" <shiyi...@gmail.com> wrote:
> >                query.put("p", "TestArea");
> >                query.append("p", "TestSquare");
>
> why you use different methods here? put and append.
>
> 2012/4/26Ronnie<ankur...@tcs.com>:

H.J

unread,
Apr 26, 2012, 4:44:41 AM4/26/12
to mongod...@googlegroups.com
As Andreas said, "and all queries combined using an implicit AND (which is the
default in MongoDB)."

query.append("a":1);
query.append("b":2);

=
where a = 1 and b = 2

Ronnie

unread,
Apr 26, 2012, 4:55:34 AM4/26/12
to mongodb-user
But if the keys are same?In that case will this work?


On Apr 26, 1:44 pm, "H.J" <shiyi...@gmail.com> wrote:
> As Andreas said, "and all queries combined using an implicit AND (which is the
> default in MongoDB)."
>
> query.append("a":1);
> query.append("b":2);
>
> =
> where a = 1 and b = 2
>

H.J

unread,
Apr 26, 2012, 5:03:56 AM4/26/12
to mongod...@googlegroups.com
If the keys are same, it will return nothing.
The query should be correct first.

{$or:[{a:"urn:ogc:def:phenomenon:OGC:1.0.30:test1"},
{a:"urn:ogc:def:phenomenon:OGC:1.0.30:test2"},
{$and:[{s:"TestArea"},{p:"TestSquare"}]}
]}

=

a = 'XX' or a = 'XX' or (s = 'XX' and p = 'XX')

Scott Hernandez

unread,
Apr 26, 2012, 11:45:39 AM4/26/12
to mongod...@googlegroups.com
You don't need the $and here, just {s:"TestArea", p:"TestSquare"}

2012/4/26 H.J <shiy...@gmail.com>:

hitesh kumar

unread,
Apr 26, 2012, 12:58:44 PM4/26/12
to mongod...@googlegroups.com
BasicDBObject A = new BasicDBObject("a","YOUR VALUE");
BasicDBObject B = new BasicDBObject("a","YOUR VALUE");
DBObject = QueryBuilder().start().or(A,B).get();


I think this will work for you....
Reply all
Reply to author
Forward
0 new messages