MongoDB with Python - pyMongo - Basic III

In [1]:
from pymongo import MongoClient, ASCENDING, DESCENDING
import pprint
import random
In [2]:
con = MongoClient("mongodb://mdbuser:[email protected]:27017,cluster0-shard-00-01-mswpe.mongodb.net:27017,\
cluster0-shard-00-02-mswpe.mongodb.net:27017/Cluster0?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true")
con.list_database_names()
Out[2]:
['Cluster0', 'admin', 'local']
In [3]:
db = con.Cluster0
db.list_collection_names()
Out[3]:
['user', 'movies_scratch', 'testidx', 'movies_initial']
In [4]:
#db.user.insert_one({'Name':'Raina','Age':25, 'City':'Agra','Provice':'UP', 'grades':[11, 13, 9, 14],
#                       'details':[{'grade':9, 'mean':17},{'grade':13, 'mean':18}]})

Match an Embedded Document or Array
Till now, we have seen different type of find and update operation. What is the syntax
if want to match a embedded document with field order

In [5]:
pprint.pprint(list(db.user.find({'State':'UP'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'grades': [15, 15, 17, 15]},
 {'Age': 28,
  'City': 'Lucknow',
  'Marital_Status': None,
  'Name': 'Rashi',
  'State': 'UP',
  'Subject': ['Hindi', 'English', 'Math'],
  '_id': ObjectId('5c27f207802bd99ce0af5151'),
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'grades': [18, 17, 19, 15]}]
In [6]:
pprint.pprint(list(db.user.find({'$or':[{'Name':'Rashi'}, {'Name':'Anu'}, {'Name':'Ashley'}]})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'grades': [15, 15, 17, 15]},
 {'Age': 23,
  'City': 'Delhi',
  'Job': 'Teacher',
  'Name': 'Anu',
  '_id': ObjectId('5c26fee1802bd99ce0af5150')},
 {'Age': 28,
  'City': 'Lucknow',
  'Marital_Status': None,
  'Name': 'Rashi',
  'State': 'UP',
  'Subject': ['Hindi', 'English', 'Math'],
  '_id': ObjectId('5c27f207802bd99ce0af5151'),
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'grades': [18, 17, 19, 15]}]

Find the documet which has grade 19 or 13 element

In [7]:
list(db.user.find({'grades':19}))
Out[7]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]
In [8]:
list(db.user.find({'grades':13}))
Out[8]:
[{'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [9]:
list(db.user.find({'$or':[{'grades':13},{'grades':19}]} ))
Out[9]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [10]:
list(db.user.find({'grades':{'$gt':18}}))
Out[10]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]
In [11]:
#condition shoule be satisfied by any one element pr different element
# as we can see there are no element array which can satisfy > 19, but have <24. ( False and True : False) hence no result
# either a single array element meets these condition or any combination of array elements meets the conditions.
list(db.user.find({'grades':{'$gt':19, '$lt':23}}))  
Out[11]:
[]
In [12]:
 # same above scenario at least one element is >18 and all are <24 (T and T : T)
list(db.user.find({'grades':{'$gt':18, '$lt':23}}))  
Out[12]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]
In [13]:
# what if we can to satisfy both conditions by at least one element
# at least one array element satisfies all the specified criteria.
list(db.user.find({'grades':{'$elemMatch':{'$gt':9, '$lt':14}}}))  
Out[13]:
[{'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [14]:
# Query on Specific element (index)
# first index should follow both condition
list(db.user.find({'grades.0':{'$gt':9, '$lt':14}}))  
Out[14]:
[{'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [15]:
# Query on Specific element (index)
# first index should follow both condition
list(db.user.find({'grades.0':{'$gt':11, '$lt':14}}))  
Out[15]:
[]

Matching array exactly

In [16]:
list(db.user.find({'grades':15}))
Out[16]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]
In [17]:
list(db.user.find({'grades':[15, 15, 17, 15]}))
Out[17]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']}]
In [18]:
# get the user who is having Math subject 
list(db.user.find({'Subject':'Math'}))
Out[18]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [19]:
#If want to get user who is having French and Math both
list(db.user.find({'Subject':['English', 'French']}))
Out[19]:
[]
In [20]:
# the above query returns NULL as it is looking for exact match with element order as well
# so querying without any order we use $all operator
list(db.user.find({'Subject':{'$all':['English', 'French']}}))
Out[20]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']}]
In [21]:
# though there is another way to fetch the same with $and operator
list(db.user.find({'$and':[{'Subject':'English'}, {'Subject':'French'}]}))
Out[21]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']}]
In [22]:
# Query on array size
list(db.user.find({'Subject':{'$size':3}}))
Out[22]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]
In [23]:
# Query on array size
list(db.user.find({'Subject':{'$size':2}}))
Out[23]:
[{'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]

Match a field in embed document

In [24]:
list(db.user.find({'details.grade':10}))
Out[24]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']}]
In [25]:
list(db.user.find({'details.mean':17}))
Out[25]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [26]:
# Get documents where mean=17 (any element) and grade=13 (any element)
list(db.user.find({'$and':[{'details.mean':17}, {'details.grade':13}]}))
Out[26]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]

Query on specific element

In [27]:
# fetching records of 2nd element of details have grade 17
list(db.user.find({'details.1.grade':17}))
Out[27]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]
In [28]:
# fetch record/s if 1st element grade is less then 10
list(db.user.find({'details.0.grade':{'$lt':10}}))
Out[28]:
[{'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [29]:
# fetch record/s if 1st element mean is 18
list(db.user.find({'details.1.mean':18}))
Out[29]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [30]:
# fetch record/s with multiple conditions on document
list(db.user.find({'$and':[{'details.1.mean':18}, {'details.1.grade':{'$lt':15}}]}))
Out[30]:
[{'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]

fetch records if any one element of array of document match the conditions
Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.

In [31]:
list(db.user.find({'details.1.mean':18}))
Out[31]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [32]:
list(db.user.find({'details.mean':17, 'details.grade':13}))
Out[32]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [33]:
# $elemMatch is required if you you want to search all conditions in same document
list(db.user.find({'details':{'$elemMatch':{'mean':17, 'grade':18}}}))
Out[33]:
[{'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]

Find exact emded document

In [34]:
list(db.user.find({'details':[{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}]}))
Out[34]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']}]

What if query on not existed field

In [35]:
list(db.user.find({'LName':'Singh'}))
Out[35]:
[]

How to check NULL : $type operator (Null =10)

In [36]:
list(db.user.find({'Name':{'$type':10}}))
Out[36]:
[]
In [37]:
#Let's insert one field with NULL
db.user.update_many({'State':'UP'}, {'$set':{'Marital_Status': None }})
Out[37]:
<pymongo.results.UpdateResult at 0x2c2b3eef848>
In [38]:
list(db.user.find({'Marital_Status':{'$type':10}}))
Out[38]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']}]

Check field existence - $exist

In [39]:
db.user.count_documents({})
Out[39]:
77
In [40]:
db.user.count_documents({'Marital_Status':{'$type':10}})
Out[40]:
2
In [41]:
db.user.count_documents({'Marital_Status':{'$exists':True}})
Out[41]:
2
In [42]:
db.user.count_documents({'Marital_Status':{'$exists':False}})
Out[42]:
75
In [43]:
db.user.count_documents({'details':{'$exists':True}})
Out[43]:
3
In [44]:
list(db.user.find({'details':{'$exists':True}}))
Out[44]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]

projecting Columns

In [45]:
list(db.user.find({'grades':{'$gt':13}}))
Out[45]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'City': 'Kanpur',
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['French', 'English', 'Art']},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'City': 'Lucknow',
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}],
  'State': 'UP',
  'Marital_Status': None,
  'Subject': ['Hindi', 'English', 'Math']},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'City': 'Agra',
  'Provice': 'UP',
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}],
  'Subject': ['French', 'Math']}]
In [46]:
list(db.user.find({'grades':{'$gt':13}}, {'Name':1, 'Age':1, 'grades':1}))
Out[46]:
[{'_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'Name': 'Ashley',
  'Age': 26,
  'grades': [15, 15, 17, 15]},
 {'_id': ObjectId('5c27f207802bd99ce0af5151'),
  'Name': 'Rashi',
  'Age': 28,
  'grades': [18, 17, 19, 15]},
 {'_id': ObjectId('5c2824cc802bd95eb47c5035'),
  'Name': 'Raina',
  'Age': 25,
  'grades': [11, 13, 9, 14]}]
In [47]:
# if want to remove any column
list(db.user.find({'grades':{'$gt':13}}, {'Name':1, 'Age':1, 'grades':1, '_id':0}))
Out[47]:
[{'Name': 'Ashley', 'Age': 26, 'grades': [15, 15, 17, 15]},
 {'Name': 'Rashi', 'Age': 28, 'grades': [18, 17, 19, 15]},
 {'Name': 'Raina', 'Age': 25, 'grades': [11, 13, 9, 14]}]
In [48]:
# same can be apply on embeded document
list(db.user.find({'details.grade':{'$gt':12}}, {'Name':1, 'Age':1, 'grades':1, 'details':1, '_id':0}))
Out[48]:
[{'Name': 'Ashley',
  'Age': 26,
  'grades': [15, 15, 17, 15],
  'details': [{'grade': 10, 'mean': 15}, {'grade': 13, 'mean': 17}]},
 {'Name': 'Rashi',
  'Age': 28,
  'grades': [18, 17, 19, 15],
  'details': [{'grade': 18, 'mean': 17}, {'grade': 17, 'mean': 18}]},
 {'Name': 'Raina',
  'Age': 25,
  'grades': [11, 13, 9, 14],
  'details': [{'grade': 9, 'mean': 17}, {'grade': 13, 'mean': 18}]}]
In [49]:
# if want to display specific embeded key
list(db.user.find({'details.grade':{'$gt':12}}, {'Name':1, 'Age':1, 'grades':1, 'details.mean':1, '_id':0}))
Out[49]:
[{'Name': 'Ashley',
  'Age': 26,
  'grades': [15, 15, 17, 15],
  'details': [{'mean': 15}, {'mean': 17}]},
 {'Name': 'Rashi',
  'Age': 28,
  'grades': [18, 17, 19, 15],
  'details': [{'mean': 17}, {'mean': 18}]},
 {'Name': 'Raina',
  'Age': 25,
  'grades': [11, 13, 9, 14],
  'details': [{'mean': 17}, {'mean': 18}]}]

if we want to slice an array key

In [50]:
# this will print last element from the grades array
list(db.user.find({'details.grade':{'$gt':12}}, {'Name':1, 'Age':1, 'grades':{'$slice':-1}, 'details.mean':1, '_id':0})) 
Out[50]:
[{'Name': 'Ashley',
  'Age': 26,
  'grades': [15],
  'details': [{'mean': 15}, {'mean': 17}]},
 {'Name': 'Rashi',
  'Age': 28,
  'grades': [15],
  'details': [{'mean': 17}, {'mean': 18}]},
 {'Name': 'Raina',
  'Age': 25,
  'grades': [14],
  'details': [{'mean': 17}, {'mean': 18}]}]
In [51]:
# this will print last 2 element from the grades array
list(db.user.find({'details.grade':{'$gt':12}}, {'Name':1, 'Age':1, 'grades':{'$slice':-2}, 'details.mean':1, '_id':0})) 
Out[51]:
[{'Name': 'Ashley',
  'Age': 26,
  'grades': [17, 15],
  'details': [{'mean': 15}, {'mean': 17}]},
 {'Name': 'Rashi',
  'Age': 28,
  'grades': [19, 15],
  'details': [{'mean': 17}, {'mean': 18}]},
 {'Name': 'Raina',
  'Age': 25,
  'grades': [9, 14],
  'details': [{'mean': 17}, {'mean': 18}]}]
In [52]:
# this will print last 2 element from the grades array (print index 1,2,3)
list(db.user.find({'details.grade':{'$gt':12}}, {'Name':1, 'Age':1, 'grades':{'$slice':[1,3]}, 'details.mean':1, '_id':0})) 
Out[52]:
[{'Name': 'Ashley',
  'Age': 26,
  'grades': [15, 17, 15],
  'details': [{'mean': 15}, {'mean': 17}]},
 {'Name': 'Rashi',
  'Age': 28,
  'grades': [17, 19, 15],
  'details': [{'mean': 17}, {'mean': 18}]},
 {'Name': 'Raina',
  'Age': 25,
  'grades': [13, 9, 14],
  'details': [{'mean': 17}, {'mean': 18}]}]