find_one query returns just the fields instead of an entry - mongodb

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)

Related

How do I make a mongo query for something that is not in a subdocument array of heterodox size?

I have a mongodb collection full of 65k+ documents, each one with a properties named site_histories. The value of it is an array that might be empty, or might not be. If it is not empty, it will have one or more objects similar to this:
"site_histories" : "[{\"site_id\":\"129373\",\"accepted\":\"1\",\"rejected\":\"0\",\"pending\":\"0\",\"user_id\":\"12743\"}]"
I need to make a query that will look for every instance in the collection of a document that does not have a given user_id.
I'm pretty new to Mongo, so I was trying to make a query that would find every instance that does have the given user_id, which I was then planning on adding a "$ne" to, but even that didn't work. This is the query I was using that didn't work:
db.test.find({site_histories: { $elemMatch: {user_id: '12743\' }}})
So can anyone tell me why this query didn't work? And can anyone help me format a query that will do what I need the final query to do?
If your site_histories really is an array, it should be as simple as doing:
db.test.find({"site_histories.user_id": "12743"})
That looks in all the elements of the array.
However, I'm a bit scared of all those backslashes. If site_histories is a string, that won't work. It would mean that the schema is poorly designed, you'd maybe try with $regex

Mongo Get Count While Returning Whole Documents and Should Queries

I am new to Mongo and can't seem to figure out the following after reading posts and the documentation. I am executing the following query:
db.collection.find({'name':'example name'})
Which returns 14 results. I can get the count of correctly by executing:
db.collection.find({'name':'example name'}).count()
However, I want to return the full documents and the count in a single query, similar to the way Elasticsearch does. Is there anyway to do this.
Additionally, is there any equivalence to Elasticsearch's Bool should query (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html). Essentially I would want to rank the results, so that those with attribute 'onSale=True' are returned before 'onSale=False'.
I'm not sure about your second question, whether MongoDB provides some mechanism equivalent to Elasticsearch's Bool should query.
But for your 1st question, I think you can use Cursor.
var cursor = db.collection.find({'name':'example name'});
Once you've got the cursor, you can use it for getting the count in the following way:
cursor.count()
as well as for getting the documents wrapped in an array in the following way:
cursor.toArray()
For more info on cursor, please see the below mentioned link:
http://docs.mongodb.org/manual/tutorial/iterate-a-cursor/

I am a new user of Mongo Db, please let me know the difference in executing both queries

Hi I found that the output of the two queries was same, but I want to know is there any difference in executing the query.
First:
db.collectionname.find({}).pretty()
Second:
db.offers.find().pretty()
There is no difference between these two queries.
db.collectionname.find({}).pretty()
You are not giving any query params here so the results are same like
db.collectionname.find().pretty()
In short: Both function in the same way.
1) db.collectionname.find({})
Here you are not specifying any query parameters and its just an empty document {} so it will return you all the documents present in the collection
2) db.offers.find()
Here you didn't specify any queries. So it need not even look at the parameters, it will just print you all the documents in the collection. find() short form of find({})

Pymongo returns subdocs as dicts instead of SONs?

Is there a way to have pymongo return subdocs as SONs so they can be successfully passed back in for find queries? GridFS is returning the file._id as a dict instead of a SON; so, the subsequent call to grid_file.GridOut.read is failing to find the grid file. (I know, using dicts as _id is asking for trouble, but I'm dealing w/ an existing db/system.)
I don't see how I can intervene to coerce the file._id into the correctly ordered SON.
The MongoClient.document_class field controls this! Just add document_class=bson.son.SON, to the constructor

How do I select this field in mongodb

I know it is probably a simple solution but for some reason I am having trouble calling it... I tried querying it then calling
user.connected_accounts.facebook but that does not work... I need to get hte number out.
Here is the code:
"connected_accounts":[{"facebook":"5555555"}],
Here is my mongoose schema:
connected_accounts: {type: Array}
For you question, I want to know you query clause, in mongodb console, it is different when you are using db.collection.find method and db.collection.findOne method, find method will return an collections, but findOne method will return one object(docment).
So if you are using find, you can get result like: results[0].connected_accounts[0].facebook , if you are using findOne, you can get result like: results.connected_accounts[0].facebook.
I had to just called connected_account[0].facebook to get the value.