Mongo db spatial query support with java

737 views
Skip to first unread message

Ronnie

unread,
Apr 17, 2012, 4:12:19 AM4/17/12
to mongodb-user
Hi All,
I have got some answers regarding "Mongo db spatial query support with
java" , from Spring Data - MongoDB.

However I do not know spring.

Could any one provide some other way out.?

Thanks and Regards

H.J

unread,
Apr 17, 2012, 4:32:56 AM4/17/12
to mongod...@googlegroups.com
use same sample data in this blog:http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/

In my opinion, it is same as other queries.

DBCollection loc = db.getCollection("location"); 
BasicDBList v1 = new BasicDBList();
v1.add(0);
v1.add(0);
// db.location.find( {position: { $near: [0,0], $maxDistance: 0.75  } } )
BasicDBObject query = new BasicDBObject();
query.put("position", BasicDBObjectBuilder.start().append("$near",v1).append("$maxDistance", 0.75).get());
DBCursor cursor1 = loc.find(query);

2012/4/17 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 17, 2012, 4:44:01 AM4/17/12
to mongodb-user
Thanks HJ,

Got your point.

My concern is that :

I can use the mongo shell to insert spatial data like :

>db.places.insert({loc : [50,50]})

I am using the following code base through java :

BasicDBObject doc = new BasicDBObject();
//Location loctn = new Location();
//System.out.println(sensorlocation +" "
+loctn.getMongoCompatiblePositionAttr(sensorlocation));
doc.put("loc", "50,50");
coll.insert(doc);

Here "50,50" is the lat/long data. I have doubt that inserting like
this will not provide correct data at the time of spatial query.

I wanna avoid spring-mongo api.Could you you please guide on this?

Thanks in advance.


On Apr 17, 1:32 pm, "H.J" <shiyi...@gmail.com> wrote:
> use same sample data in this blog:http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-...
>
> In my opinion, it is same as other queries.
>
>  DBCollection loc = db.getCollection("location");
>  BasicDBList v1 = new BasicDBList();
>  v1.add(0);
>  v1.add(0);
>  // db.location.find( {position: { $near: [0,0], $maxDistance: 0.75  } } )
>  BasicDBObject query = new BasicDBObject();
>  query.put("position",
> BasicDBObjectBuilder.start().append("$near",v1).append("$maxDistance",
> 0.75).get());
>  DBCursor cursor1 = loc.find(query);
>
> 2012/4/17 Ronnie <ankur...@tcs.com>

H.J

unread,
Apr 17, 2012, 4:58:29 AM4/17/12
to mongod...@googlegroups.com
I'm afraid inserting in this way via Java wouldn't work. It will be treated as a string value instead of long/lat value.

"50,50" can be put into an array.

2012/4/17 Ronnie <anku...@tcs.com>



--
尽人事,听天命!

Ronnie

unread,
Apr 17, 2012, 5:02:14 AM4/17/12
to mongodb-user
Thanks .

Could you provide with some sample java code(means converting the
string to array and then inserting to mongo) , the problem which I
addressed.It will be great if you do so.

Thanks again.

On Apr 17, 1:58 pm, "H.J" <shiyi...@gmail.com> wrote:
> I'm afraid inserting in this way via Java wouldn't work. It will be treated
> as a string value instead of long/lat value.
>
> "50,50" can be put into an array.
>
> 2012/4/17 Ronnie <ankur...@tcs.com>

Ronnie

unread,
Apr 17, 2012, 5:07:11 AM4/17/12
to mongodb-user
Thanks so much.Will keep in touch if any further issues.

On Apr 17, 1:58 pm, "H.J" <shiyi...@gmail.com> wrote:
> I'm afraid inserting in this way via Java wouldn't work. It will be treated
> as a string value instead of long/lat value.
>
> "50,50" can be put into an array.
>
> 2012/4/17 Ronnie <ankur...@tcs.com>

Ronnie

unread,
Apr 27, 2012, 8:13:39 AM4/27/12
to mongodb-user
Hi,

Another challange:

SELECT DISTINCT x,
* AsText(geom) AS foi_geom, SRID(geom) AS foi_srid, feature_type,
schema_link FROM testtable
* WHERE Intersects(ENVELOPE(GeometryFromText('MULTIPOINT ((7 50),
(10 53))', 4326)), testtable.geom)

How to execute the above query in Mongo java.

Need your help HJ.

Thanks and Regards in advance.

On Apr 17, 1:58 pm, "H.J" <shiyi...@gmail.com> wrote:
> I'm afraid inserting in this way via Java wouldn't work. It will be treated
> as a string value instead of long/lat value.
>
> "50,50" can be put into an array.
>
> 2012/4/17 Ronnie <ankur...@tcs.com>
Message has been deleted

Scott Hernandez

unread,
Apr 27, 2012, 10:05:33 AM4/27/12
to mongod...@googlegroups.com
This has the flavor of spam/fishing. Please include a
description/question in any post. This will be deleted. If this was a
real question, please don't just link to untrusted source.

On Fri, Apr 27, 2012 at 6:15 AM, e kartheeka <karthe...@gmail.com> wrote:
> http://indiasolo.com/46/king004/

H.J

unread,
Apr 27, 2012, 10:48:44 AM4/27/12
to mongod...@googlegroups.com
I'm sorry that I have no idea for SqlServer method.

Ronnie

unread,
Apr 30, 2012, 5:52:03 AM4/30/12
to mongodb-user
Thanks .
Any way can you convert this into java syntax?

> box = [[8.1,50.5],[9.1,54.5]]
[ [ 8.1, 50.5 ], [ 9.1, 54.5 ] ]
> db.Testcollection.find({"foi_geom":{"$within":{"$box":box}}})

Thanks in advance

On Apr 27, 7:48 pm, "H.J" <shiyi...@gmail.com> wrote:
> I'm sorry that I have no idea for SqlServer method.
>
> 在 2012年4月27日 下午8:13,Ronnie <ankur...@tcs.com> 写道:
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > Another challange:
>
> > SELECT DISTINCT x,
> >         * AsText(geom) AS foi_geom, SRID(geom) AS foi_srid, feature_type,
> > schema_link FROM testtable
> >         * WHERE Intersects(ENVELOPE(GeometryFromText('MULTIPOINT ((7 50),
> > (10 53))', 4326)), testtable.geom)
>
> > How to execute the above query in Mongo java.
>
> > Need your help HJ.
>
> > Thanks and Regards in advance.
>
> > On Apr 17, 1:58 pm, "H.J" <shiyi...@gmail.com> wrote:
> >> I'm afraid inserting in this way via Java wouldn't work. It will be treated
> >> as a string value instead of long/lat value.
>
> >> "50,50" can be put into an array.
>
> >> 2012/4/17 Ronnie <ankur...@tcs.com>
>
> >> > Thanks HJ,
>
> >> > Got your point.
>
> >> > My concern is that :
>
> >> > I can use the mongo shell to insertspatialdata like :
> >> > > > I have got some answers regarding "Mongo dbspatialquery support with

Ronnie

unread,
Apr 30, 2012, 6:59:30 AM4/30/12
to mongodb-user
I have tried the following code , but unfortunately null pointer
exception is thrown.

QueryBuilder.start().withinBox(8.10, 50.50, 9.10, 54.50).get()

But with the same co-ordinates I am getting results from the MongoDB
shell.

Thanks in advance.

Marc

unread,
Apr 30, 2012, 11:09:07 AM4/30/12
to mongodb-user
A similar question was asked last week regarding how to build a
$within query with the Java driver:
http://groups.google.com/group/mongodb-user/browse_thread/thread/a76be48e5d11711e

There is an example there of how to build the query document in the
Java driver. The object 'myPolygon' in the example is of datatype
ArrayList<ArrayList<Integer>>

Hopefully the example will allow you to build your desired $within
query. I am not familiar with the "QueryBuilder" object that you have
used in your example. If you can come up with a less verbose way to
perform a $within query than building an ArrayList of ArrayLists of
integers point by point, please share it here, so the Community may
have the benefit of your experience.
Thanks.

H.J

unread,
May 1, 2012, 10:56:39 AM5/1/12
to mongod...@googlegroups.com
Thank you Marc!

Ronnie, may you try this one?

DBCursor cur = coll.find(new
QueryBuilder().start("foi_geom").withinBox(8.1,9.5,55.1,54.5).get());

Sorry for late response, these days are holidays for Labor Day in China.

2012/4/30 Marc <ma...@10gen.com>:

Ronnie

unread,
May 2, 2012, 1:29:51 AM5/2/12
to mongodb-user
Thanks , but I am getting this exception :

Exception in thread "main" com.mongodb.MongoException: can't find
special index: 2d for: { foi_geom: { $within: { $box: [ [ 9, 51 ],
[ 12, 54 ] ] } } }
at com.mongodb.MongoException.parse(MongoException.java:82)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:303)
at com.mongodb.DBCursor._check(DBCursor.java:356)
at com.mongodb.DBCursor._hasNext(DBCursor.java:486)
at com.mongodb.DBCursor.hasNext(DBCursor.java:511)
at
com.tcs.mongo.util.MongoSpacialQuery.getFeatureOfInterestsInaBBox(MongoSpacialQuery.java:
103)
at com.tcs.mongo.util.MongoSpacialQuery.main(MongoSpacialQuery.java:
35)
I have indexed through shell like as follows:
db.BLOOD_PRESSURE.ensureIndex({foi_geom : "2d"})

If the above is not working then how should I do the indexing?

Thanks in advance.

On May 1, 7:56 pm, "H.J" <shiyi...@gmail.com> wrote:
> Thank you Marc!
>
> Ronnie, may you try this one?
>
> DBCursor cur = coll.find(new
> QueryBuilder().start("foi_geom").withinBox(8.1,9.5,55.1,54.5).get());
>
> Sorry for late response, these days are holidays for Labor Day in China.
>
> 2012/4/30 Marc <m...@10gen.com>:
>
>
>
>
>
>
>
>
>
> > A similar question was asked last week regarding how to build a
> > $within query with the Java driver:
> >http://groups.google.com/group/mongodb-user/browse_thread/thread/a76b...
>
> > There is an example there of how to build the query document in the
> > Java driver.  The object 'myPolygon' in the example is of datatype
> > ArrayList<ArrayList<Integer>>
>
> > Hopefully the example will allow you to build your desired $within
> > query.  I am not familiar with the "QueryBuilder" object that you have
> > used in your example.  If you can come up with a less verbose way to
> > perform a $within query than building an ArrayList of ArrayLists of
> > integers point by point, please share it here, so the Community may
> > have the benefit of your experience.
> > Thanks.
>
> > --
> > 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.

Nat

unread,
May 2, 2012, 1:38:55 AM5/2/12
to mongod...@googlegroups.com
Try dropping and recreating index again and check out the log file. You might have some invalid data in that field.

H.J

unread,
May 2, 2012, 1:41:37 AM5/2/12
to mongod...@googlegroups.com
Would you please provide some documents sample? And your java driver
version and mongodb version?

It works here.
java dirver 2.7.3, mongodb 2.1.1-pre

http://pastebin.com/diKny2HF

2012/5/2 Ronnie <anku...@tcs.com>:

Ronnie

unread,
May 2, 2012, 1:55:49 AM5/2/12
to mongodb-user
I am using MongoDB Version 2.04 , from the shell the following query
is executed.

But when trying from Java , it fails and throws exception.

Exception in thread "main" com.mongodb.MongoException: can't find
special index: 2d for: { foi_geom: { $within: { $box: [ [ 9, 51 ],
[ 12, 54 ] ] } } }

On May 2, 10:41 am, "H.J" <shiyi...@gmail.com> wrote:
> Would you please provide some documents sample? And your java driver
> version and mongodb version?
>
> It works here.
> java dirver 2.7.3, mongodb 2.1.1-pre
>
> http://pastebin.com/diKny2HF
>
> 2012/5/2Ronnie<ankur...@tcs.com>:

H.J

unread,
May 2, 2012, 1:58:35 AM5/2/12
to mongod...@googlegroups.com
would you please provide your mongo shell script?

2012/5/2 Ronnie <anku...@tcs.com>:

Ronnie

unread,
May 2, 2012, 2:04:15 AM5/2/12
to mongodb-user
> dbTestcol.ensureIndex({ foi_geom : "2d" })

> box = [[8.1,50.5],[9.1,54.5]]
[ [ 8.1, 50.5 ], [ 9.1, 54.5 ] ]
> db.Testcol.find({"foi_geom":{"$within":{"$box":box}}})

I am getting the result in shell.

On May 2, 10:58 am, "H.J" <shiyi...@gmail.com> wrote:
> would you please provide your mongo shell script?
>
> 2012/5/2Ronnie<ankur...@tcs.com>:

H.J

unread,
May 2, 2012, 2:08:36 AM5/2/12
to mongod...@googlegroups.com
What I mean is your mongo shell for real Java code, which is for
collection BLOOD_PRESSURE
In mongo shell you are operating on collection "Testcol" . I believe
it works on test env because the data for testing is clear and simple.


2012/5/2 Ronnie <anku...@tcs.com>:

Ronnie

unread,
May 2, 2012, 2:29:25 AM5/2/12
to mongodb-user
Shell script :
db.BLOOD_PRESSURE.ensureIndex({ foi_geom : "2d" })

> box = [[8.1,50.5],[9.1,54.5]]
[ [ 8.1, 50.5 ], [ 9.1, 54.5 ] ]
> db.BLOOD_PRESSURE.find({"foi_geom":{"$within":{"$box":box}}})

The above script returns data.

Java code :

ArrayList<ArrayList<Integer>> mybox = new
ArrayList<ArrayList<Integer>>();
ArrayList<Integer> point1 = new ArrayList<Integer>();
point1.add(9);
point1.add(51);
ArrayList<Integer> point2 = new ArrayList<Integer>();
point2.add(12);
point2.add(54);
mybox.add(point1);
mybox.add(point2);
BasicDBObject myQuery = new BasicDBObject();
BasicDBObject withinQuery = new BasicDBObject();
withinQuery.append("$within", new BasicDBObject("$box",mybox));
myQuery.append("foi_geom", withinQuery);
for (String s : colls) {
DBCollection coll = db.getCollection(s);
DBCursor c = coll.find(myQuery);
while(c.hasNext()){
System.out.println(c.next().toString());
}
}

The above code returns the following exception :

Exception in thread "main" com.mongodb.MongoException: can't find
special index: 2d for: { foi_geom: { $within: { $box: [ [ 9, 51 ],
[ 12, 54 ] ] } } }

On May 2, 11:08 am, "H.J" <shiyi...@gmail.com> wrote:
> What I mean is your mongo shell for real Java code, which is for
> collection BLOOD_PRESSURE
> In mongo shell you are operating on collection "Testcol" . I believe
> it works on test env because the data for testing is clear and simple.
>
> 2012/5/2Ronnie<ankur...@tcs.com>:

H.J

unread,
May 2, 2012, 2:36:44 AM5/2/12
to mongod...@googlegroups.com
I think it's better to try each collection one by one, maybe some of
them don't fulfill geo searching.

2012/5/2 Ronnie <anku...@tcs.com>:

Ronnie

unread,
May 2, 2012, 2:52:30 AM5/2/12
to mongodb-user
Now i tried with a different db :Refer to the script below:

> db.testCol.ensureIndex({ foi_geom : "2d" })
> db.testCol.find({"foi_geom":{"$within":{"$box":box}}})
{ "_id" : ObjectId("4fa0d6a21087b08a3095ad79"), "foi_geom" : [ 8.2,
53.2 ] }
> db.testCol.drop()
true
> db.testCol.insert({"foi_geom":[8,53]})
> db.testCol.ensureIndex({ foi_geom : "2d" })
> box = [[8.1,50.5],[9.1,54.5]]
[ [ 8.1, 50.5 ], [ 9.1, 54.5 ] ]
> db.testCol.find({"foi_geom":{"$within":{"$box":box}}})
> box = [[7,50],[9,54]]
[ [ 7, 50 ], [ 9, 54 ] ]
> db.testCol.find({"foi_geom":{"$within":{"$box":box}}})
{ "_id" : ObjectId("4fa0d7481087b08a3095ad7b"), "foi_geom" : [ 8,
53 ] }

I again got data.

Then I tried the following java code :This time no exception but got
no data as well:

Mongo m = null;
try {
m = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DB db = m.getDB("testdatabase");
System.out.println(db);
DBCollection coll = db.getCollection("testCol");
System.out.println(coll);
//myPolygon = [[0,0], [0,4], [2,6], [4,4], [4,0]];//
8.1,9.5,55.1,54.5//8.3 , 53.5

ArrayList<ArrayList<Integer>> mybox = new
ArrayList<ArrayList<Integer>>();
ArrayList<Integer> point1 = new ArrayList<Integer>();
point1.add(7);
point1.add(50);
ArrayList<Integer> point2 = new ArrayList<Integer>();
point2.add(9);
point2.add(54);
mybox.add(point1);
mybox.add(point2);
BasicDBObject myQuery = new BasicDBObject();
BasicDBObject withinQuery = new BasicDBObject();
withinQuery.append("$within", new BasicDBObject("$box",mybox));
myQuery.append("foi_geom", withinQuery);//"foi_geom"
//for (String s : colls) {
DBCursor c = coll.find(myQuery);
System.out.println(myQuery);
while(c.hasNext()){
System.out.println(c.next().toString());
}

Can put some light on it?

Thanks in advance.This has become really annoying.


On May 2, 11:36 am, "H.J" <shiyi...@gmail.com> wrote:
> I think it's better to try each collection one by one, maybe some of
> them don't fulfill geo searching.
>
> 2012/5/2Ronnie<ankur...@tcs.com>:

H.J

unread,
May 2, 2012, 3:02:26 AM5/2/12
to mongod...@googlegroups.com
make sure both mongo shell and java operation are in the same DB "testdatabase"

In mongo shell,

use testdatabase
db.testCol.find({"foi_geom":{"$within":{"$box":box}}})

I believe no data return.

2012/5/2 Ronnie <anku...@tcs.com>:

Ronnie

unread,
May 2, 2012, 4:32:24 AM5/2/12
to mongodb-user
Finally it worked.

I deleted the collection and re-built it.

Worked!

May be some silly mistakes, n e ways sorry for so much queries and
thanks for your proactive help as ever.



On May 2, 12:02 pm, "H.J" <shiyi...@gmail.com> wrote:
> make sure both mongo shell and java operation are in the same DB "testdatabase"
>
> In mongo shell,
>
> use testdatabase
> db.testCol.find({"foi_geom":{"$within":{"$box":box}}})
>
> I believe no data return.
>
> 2012/5/2Ronnie<ankur...@tcs.com>:
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages