I am using MongoDB and pymongo
to create a database and store data in them. I have a list that I wish to store in a database. And as far as I understand, I would have to use Python dict to do so and it gets stored as an array in the database. Now after storing it, to perform other operations such as plotting, mathematical operations etc. I want to set that array as a variable. But I cannot find a way to do so. Simply setting that array as a list/array variable is good enough. The variable ‘results’ does not print the array.
import pymongo
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db= client['l']
collection=db['data']
x= [1,2,3,4,5]
record = {'_id': 'x', 'values': x}
db.collection.insert_one(record)
results= collection.find({'values':x})
2
Answers
First, i guess your database name is
l
and the collection in that database isdata
. To insert in your collection change yourinsert_one
line into something like this :You can also make some kind of mix using dot notation or array notation.
Then :
results
is a Cursor instance on which you can iterate to have the documents.So you can try :
results = [collection.find({'values':x})]
to have the list of all the document matching your query.Here’s an
ipython
session showing how you can retrieve your stored"values"
.