Hello and happy new year to everybody.
This question is because I have a problem and I don't know what to look for as a solution. I think that aggregation is no good here.
I am a novice level programmer starting with python 3.6 and pymongo 3.4 to handle Mongodb.
I have 50M documents inside a "lostItems" collection inside a "warehouse" database with this structure:
{
"_id": "5a4d018526f9c63894d11254",
"time": "2000-01-03T00:02:00.000Z",
"item": "sunglasses"
"level": 45,
"section": 15,
"box": 29,
"home": "CA"
}
"_id" field is the normal autogenerated key. "time" is not a key and is not enforced to be unique, but it happens to be so, for now.
Let's see if I can explain clearly the query I want:
It will order documents by "time" ("time", pymongo.DESCENDING)
It will look for, and note down somehow, every "home": "CA" for use with the second query.
It will return, for each '"home": "CA"' hit, not just that document but also the 5 previous documents in the specified order (whatever the "home" value might be) and the later 10 documents.
In other words, the query will return a total of 16 documents, that are consecutive when ordered by "time", for each '"home": "CA"' found.
My approach (as a novice) would be to query for the hits and write them down in a temporal db and then make another query to get and add the previous and later documents required. This feels a little bit complicated and mostly slow query.
import pymongo
col = pymongo.MongoClient('localhost', 27017).warehouse.lostItems
query = {"home": "CA"}
order = [ ("time", pymongo.DESCENDING) ]
try:
for doc in col.find(query).sort(order):
**"Now I don't know what to do"**
except Exception as e:
print ("Error :", type(e), e)
Could you please give me some advice and/or tell me what is this kind of query called so that I can look for more similar cases to learn about?
Thank you very much in advance.