<jupyter_start><jupyter_text>location object $\rightarrow$ GeoObject location: {      type: "Point",            coordinates: [longitude, latitude]}<jupyter_code># collections in the database
db.list_collection_names()
# creating index: 2dsphere (for location data)
db.incidents.create_index([("location", pymongo.GEOSPHERE)])
pymongo.GEOSPHERE
cursor = db.incidents.aggregate([
           {
             '$geoNear': {
                           'near': {'type': "Point", 'coordinates': [ 73.99279 , 40.719296 ]},
                           'spherical': True,
                           'distanceField': "dist.calculated",
                           'num': 5
                         }
           }, {

             '$project': {'_id': 0, 'Date':  ["$iyear", "$imonth", "$iday"], 'location': 1, 'country_txt': 1, 'gname': 1}
           }
        ]);
# printing the events
for event in cursor:
    print(event)
project = {'_id': 0, 'location': 1, 'country_txt': 1, 'gname': 1, 'weaptype1_txt': 1}
cursor = db.incidents.find(
                        {'country_txt': 'Nepal'}, 
                          project
                      ).limit(5)
for k in cursor:
    print(k)
# text search
# creating tet index for gname
db.incidents.create_index([('gname', pymongo.TEXT)])
# carried buy 'Islamic' groups
cursor = db.incidents.aggregate([
    {'$match': {'$text': {'$search': "Islamic"}}},
    {'$project': {'_id': 0, 'gname': 1, 'country_txt': 1, 'weaptype1_txt': 1}}
])
i = 0
for c in cursor:
    if i < 5:
        pprint(c)
    i += 1
print("\n========================")
print('total incidents: ', i)
# Aleppo: coordinates ~ [36, 37]
# all the incidents in Syria near Allepo
cursor = db.incidents.aggregate([
    {
        '$geoNear': {
            'near': {'type': "Point", 'coordinates': [ 36 , 37]},
            'spherical': True,
            'query': {'country_txt': 'Syria'},
            'distanceField': "dist.calculated"
        }
        
    }, {
        '$project': {'_id': 0, 'Date': {'$concat': [{'$toString': "$iyear"}, "-", {'$toString': "$imonth"}, "-", 
                    {'$toString': "$iday"}]}, 'gname': 1, 'country_txt': 1, 'location': 1} # concat (string YMD)
    }       
])
i = 0
for c in cursor:
    if i < 5:
        pprint(c)
    i += 1
print('\n==========================')
print('total incidents: ', i)
# All the fields in the collections
db.incidents.find_one()<jupyter_output><empty_output><jupyter_text>Inside United States<jupyter_code>query = {'country_txt': 'United States'}
project = {'_id': 0, 'city': 1, 'provstate': 1 , 'country_txt': 1, 'gname': 1, 'location.coordinates': 1, 'weaptype1_txt': 1,
           'nkill': 1}
cursor = db.incidents.find(query, project)
group_name = {}
i = 0
for e in cursor:
    if i < 5:
        print(e['city'], ', ', e['gname'], ', ', e['weaptype1_txt'], ', ', e['nkill'])
    i += 1
    
    # saving unique group name
    if e['gname'] not in group_name.keys():
        group_name[e['gname']] = 1
    else:
        group_name[e['gname']] += 1
print('\n============')
print(i)
# plotting
df = pd.DataFrame.from_dict({'group_name': list(group_name.keys()), 'num_incidents': list(group_name.values())})
plt.figure(figsize=(10, 5))
g = sns.barplot(x='group_name', y='num_incidents', data=df);
g.set_xlabel('Group');
g.set_xticklabels(rotation=90, labels=list(group_name.keys()));
# total numbers of kills by groups
cursor = db.incidents.aggregate([
    {'$match': {'country_txt': 'United States'}},
    {'$group': {'_id': '$gname', 'total_kill': {'$sum': "$nkill"}}},
    {'$project': {'_id': 0, 'gname': '$_id', 'total_kill': 1}},
    {'$sort': {'total_kill': 1}}
])
i = 0
groups = []
kills = []
for c in cursor:
    groups.append(c['gname'])
    kills.append(c['total_kill'])
# groups and total kill by them
df = pd.DataFrame({'gname': groups, 'total_kill': kills})
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20848422141748577, 'fraction_numerical': 0.01939989653388515, 'mean_word_length': 2.9219066937119678, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '278933', 'license': 'MIT', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1300, 'n_words': 318, 'path': 'mongoDB/mongo_geodata_query.ipynb', 'repo_name': 'rnepal2/playing_database', 'size': '3866'}