mongodb python code example

Example 1: python MongoEngine doc

# Only find users whose age is 18 or less
young_users = Users.objects(age__lte=18)
equal_18 = Users.objects(age__not__ne=18)

"""
# Only find users whose age is 18 or less
young_users = Users.objects(age__lte=18)

Available operators are as follows:

    ne – not equal to
    lt – less than
    lte – less than or equal to
    gt – greater than
    gte – greater than or equal to
    not – negate a standard check, may be used before other operators (e.g. Q(age__not__mod=(5, 0)))
    in – value is in list (a list of values should be provided)
    nin – value is not in list (a list of values should be provided)
    mod – value % x == y, where x and y are two provided values
    all – every item in list of values provided is in array
    size – the size of the array is
    exists – value for field exists
"""

Example 2: pymongo find one

>>> import pprint
>>> pprint.pprint(posts.find_one())
{u'_id': ObjectId('...'),
 u'author': u'Mike',
 u'date': datetime.datetime(...),
 u'tags': [u'mongodb', u'python', u'pymongo'],
 u'text': u'My first blog post!'}

Tags:

Go Example