0

I have inserted

{
   "apples": 430,
   "bananas": 312,
   "oranges": 525,
   "pears": 217
}

to mongodb in python. However, I tried a lot ways to get value by key

If I want to get values for "apple", how would I achieve that?

3
  • Are you using the official module for Python and MongoDB (PyMongo) or an alternative module to access your database? Commented May 27, 2017 at 17:15
  • Are you using a framework (e.g. Flask, Django, etc.)? Commented May 27, 2017 at 17:19
  • hi, I am not using any framework. I am just using regular Python. I followed tutorial api.mongodb.com/python/current/tutorial.html but could not find anything that solves my problem. Thank you Commented May 27, 2017 at 17:26

1 Answer 1

1

Values in MongoDB are stored in a json format.
If you want to query from a MongoDB database, I'd recommend using PyMongo.
I'll assume you can read by yourself how to connect to the database.
The query you're looking for is as follows

apples = db.collection.distinct("base.apples")
result = db.collection.distinct("result") 

In case apples shows in other places, ofcourse.

Sign up to request clarification or add additional context in comments.

1 Comment

@SamThapa I'm glad!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.