This is a sample document holding OHLC stock data, truncated (via python print).
{
"_id":ObjectId("4f1c567d60de7d0908000000"),
"ticker" : "msft"
"pricing":[
{
"Volume":"2094300",
"Adj Close":"85.87",
"High":"88.11",
"Low":"87.45",
"Date": ISODate("2011-01-14T00:00:00 Z"),
"Close":"88.10",
"Open":"87.74"
},
{
"Volume":"2351900",
"Adj Close":"85.81",
"High":"88.87",
"Low":"87.76",
"Date": ISODate("2011-01-13T00:00:00 Z"),
"Close":"88.04",
"Open":"88.47"
},
{
"Volume":"2732900",
"Adj Close":"86.42",
"High":"88.76",
"Low":"87.92",
"Date": ISODate("2011-01-12T00:00:00 Z"),
"Close":"88.66",
"Open":"88.02"
}
Couple questions about this:
-
Is there any way to index things within a document and not within a collection? I want to be able to easily look up data by date, maybe there is a better way to organize the data?
-
I am confused as to how to retrieve or update a specific pricing data, I tried to push in the new pricing data using:
var = collection.find({ ‘ticker’ : ticker}) //Find the correct document
var[0].update( { ‘$push’ : { ‘pricing’ : newpricingdata } }) //update that companies values
but it doesnt work. Find works fine, update doesnt.
newpricingdata looks like (python print):
[
{
'Volume':'3522600',
'Adj Close':'85.65',
'High':'85.70',
'Low':'84.96',
'Date':datetime.datetime(2012,1,20,0,0),
'Close':'85.65',
'Open':'85.45'
},
{
'Volume':'3413700',
'Adj Close':'85.80',
'High':'86.23',
'Low':'84.92',
'Date':datetime.datetime(2012,1,19,0,0),
'Close':'85.80',
'Open':'85.10'
},
]
- How would you retrieve the price of ticker msft on say 1/20/2012 via python? The mongodb documentation doesnt really give good examples of subtree operations…
If you want to query by date then:
To update pricing data, you have to call the
update()method oncollection, just as you call find() on the samecollection, and you want $pushAll sincenewpricingdatais an array:Question:
It’s not possible to query and return only matching elements of an array, mongodb will give you the whole array. Which means you’ll have to extract that element in python.