[
{
u"_id": ID('1234'),
u"attributes": {u"FN": u"John", u"LN": u"De"},
u"refs": {
u"entries": {
u"id": [u"aa1", u"3da", u"42q"],
u"type": u"Info"
}
}
},
{
u"_id": ID('1278'),
u"attributes": {u"FN": u"Trey", u"LN": u"Mullens"},
u"refs": {
u"entries": {
u"id": [u"aa1", u"3d4", u"42q"],
u"type": u"Info"
}
}
}
]
From the above list of dicts, I want to create a function to find the count of each of the entry i.e entries under refs from the two records with ID 1234 and 1278. I am facing issues to parse the dict. Pulling data from Mongo. What I am currently trying to do is creating a function that accepts the data above, which is a collection in the mongo DB and the second argument is where I want to reach to i.e. refs.entries. I am using mongo's find function to parse the data. Any support will be highly appreciated. Below is the code -
def fun(data, tar):
result = {}
i, j = tar.split('.')
for item1 in data.find({},{'_id':0, tar:1}):
for item in item1[i][j].strip("[]").split(','):
item = item.strip()
if item in result:
result[item] += 1
else:
result[item] = 1
return result