Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB $near query using Java driver
    primarykey
    data
    text
    <p>I have some shops with products on them in my MongoDB db following this data model: Shop { idShop, name, location {lat, lon}, products [] of int } If I run the following command in the mongoDB default console:</p> <blockquote> <p>db.shops.find({products:1, location:{$near:[41.391204,2.145381]}}).limit(300)</p> </blockquote> <p>to find the shops that have product nº 1 ordered by distance I get the following results:</p> <blockquote> <p>{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f5a"), "name" : "Shop1", "location" : { "lat" : 41.389915, "lon" : 2.135628 }, "products" : [ 1, 5, 4 ] }</p> <p>{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f59"), "name" : "Shop2 Center", "location" : { "lat" : 41.388191, "lon" : 2.128816 }, "products" : [ 1, 2, 3 ] }</p> <p>{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f5c"), "name" : "Shop3", "location" : { "lat" : 41.384712, "lon" : 2.172031 }, "products" : [ 6, 1 ] }</p> <p>{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f5d"), "name" : "Shop4", "location" : { "lat" : 41.384029, "lon" : 2.173936 }, "products" : [ 6, 1, 2, 3, 4, 5 ] }</p> </blockquote> <p>Now, I want the same results but using my java servlet. If I search by only product number, the results are OK but not sorted. Then, if I try to sort them by adding a new key/value condition it doesn't return nothing. The code I'm using is the following:</p> <blockquote> <p>BasicDBObject query = new BasicDBObject();</p> <p>query.put("products", Integer.parseInt(idProduct));</p> <p>QueryBuilder query2 = new QueryBuilder();</p> <p>query2.near(Double.parseDouble(lat), Double.parseDouble(lon));</p> <p>query.putAll(query2.get());</p> <p>curShops = collShops.find(query);</p> </blockquote> <p>This returns an empty JSON. I've also tryed other ways like:</p> <blockquote> <p>BasicDBObject query = new BasicDBObject();</p> <p>query.put("products", Integer.parseInt(idProduct));</p> <p>ArrayList ar = new ArrayList(2);</p> <p>ar.add(Double.parseDouble(lat));</p> <p>ar.add(Double.parseDouble(lon));</p> <p>query.put("location", new BasicDBObject("$near", ar));</p> <p>curShops = collShops.find(query);</p> </blockquote> <p>Without any luck.</p> <p>Can anyone help me with this?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload