Spring data mongoDB GeoNear query with excluding fields - mongodb

I don't know if I am doing something wrong or it is a bug.
I have the following code:
Query criteria = new Query(Criteria.where("locationTime").gte(
"date-time"));
criteria.fields().exclude("friends");
NearQuery query = NearQuery.near(point).maxDistance(maxDistance)
.num(limit).query(criteria);
GeoResults<Profile> result = mongoTemplate
.geoNear(query, Profile.class);
I am executing the query and profiles near by retrieved correctly according to distance and the "locationTime" criteria but it seems to ignore the excluded field and retrieving the profiles with their friends.
When I use simple query the exclude/include fields works perfectly.
I looked every where and could not find any resemble use-case, please let me know if i am doing something wrong.
Thanks.

There's no way to limit the fields with a geoNear command, as far as I know.
I looked into calling executeCommand to try to work around the limitations of Spring Data, but it looks like they don't even have a way to do it from the raw command.

Related

How to use limit and sort query in wso2esb dataservice experssion

I try to query documents and get last value from that result by using following query.
dbname.find({"name":"test"}).limit(1).sort({$natural:-1})
But in result I got all the documents which contains name as test.
Kindly let me know where I made mistake
Try please code
dbname.find({}).limit(1).sort({$natural:-1})
You can use below code also
dbname.findOne({name:'test'}).sort({$natural:-1})

find_one query returns just the fields instead of an entry

I'm currently trying to use pymongo's find_one query. When I run the Mongo Shell and execute a findOne query, it get a document that is returned. However when I try using pymongo's find_one query, I always seem to get just the field names instead of an actual entry.
#app.route("/borough/manhattan/")
def manhattan():
restaurantmanhattan = restaurants.find_one({'borough':'Manhattan'})
json_restaurantmanhattan = []
for restaurant in restaurantmanhattan:
json_restaurantmanhattan.append(restaurant)
json_restaurantmanhattan = json.dumps(json_restaurantmanhattan)
return json_restaurantmanhattan
Once I navigate to http://0.0.0.0:5000/borough/manhattan/ I get the following:
["cuisine","borough","name","restaurant_id","grades","address","_id"]
I believe I should be seeing a document entry that meets the query that it has Manhattan listed in the borough.
I'm at a loss as to how I should be writing the query to return that.
Can anyone explain what I'm seeing?
There are many things wrong with your view.
First as you may already know, find_one return a single document as Python dictionary. So in your for loop, you iterating the dictionary keys.
You really do not need that for loop.
import json
#app.route("/borough/manhattan/")
def manhattan():
restaurant_manhattan = restaurants.find_one({'borough':'Manhattan'})
return json.dumps(restaurant_manhattan)

Sort query in luamongo

I'm using luamongo to query mongodb. I'm quite inexperienced with mongodb, and I'm having trouble figuring out how to specify the sorting/order for the query. I don't see anything specifically about sorting on the project's wiki, but I might be missing something.
Is there a way to do this, or do I need to manually sort the returned results?
Thanks!
From https://github.com/moai/luamongo/issues/14:
To sort a query: mongodb:query("database.collection", {query = {field=value}, orderby = {field = 1}})

pymongo sort grouped results

I need to group and sort by date_published some documents stored on mongodb using pymongo.
the group part went just fine :) but when I'm addding .sort() to the query it keeps failing no matter what I tried :(
here is my query:
db.activities.group(keyf_code,cond,{},reduce_code)
I want to sort by a field called "published" (timestamp)
tried to do
db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})
and many more variations without any success
ideas anyone?
You can't currently do sort with group in MongoDB. You can use MapReduce instead which does support a sort option. There is also an enhancement request to support group with sort here.
Although MongoDB doesn't do what you want, you can always use Python to do the sorting:
result = db.activities.group(keyf_code,cond,{},reduce_code)
result = sorted(result, key=itemgetter("published"), reverse=True)

mongodb search results

I'd like to display the total of number of records a query found and then the search results below it. Currently I do this with 2 queries like so:
$total_results = $mydb->count($qry);
$r = $mydb->find($qry)
->skip($pagination*$results_per_page)
->limit($results_per_page);
Is it possible to somehow combine the count and find queries into a single call that returns both info? Or can this only be done by issuing 2 separate queries like I'm already doing?
I asked a similar question, looks like you'll have to run it twice.
How to handle pagination queries properly with mongodb and php?