Skip to main content

wrapper utility for mongodb

Project description

A wrapper library on top of pymongo.

  pip install mongoy

Connecting to Database

   from mongoy.mongo import Mongo

   connection = 'mongodb://127.0.0.1:27017'
   db_name = 'mydb''

   mgy = Mongo(connection, db_name)

Get pymongo db instance

  # get pymongo db instance
  db = mgy.get_db()

Register a collection for operations

   # Register a collection for CRUD data ops

   mgy.register_collection('users')
  # Add a document
  doc = {
    'name': 'test',
    'age': 10,
  }

  result = mgy.users.insert(doc)


  # Add multiple documents
  docs = [{
    'name': 'testuser',
    'age': 21
  }, {
    'name': 'testy',
    'age': 22
  }, {
    'name': 'uwoo',
    'age': 27
  }]

  result = mgy.users.insert(docs)
  # Find Documents

  result = mgy.users.find()

  age_22_users = mgy.users.find({
    'age': 22
  })

  others = mgy.users.find({
    'age': {'$ne': 22}
  })

  mgy.users.find({
    '_id': result[0]['_id']
  })
  from bson.objectid import ObjectId
  id = ObjectId('some-mongo-id')

  # Update single document 
  mgy.users.find_one_and_update({'_id': id}, {'$set': {'age': 30}})

  # Update all matching documents
  mgy.users.find_all_and_update({'age': {'$lt': 25}}, {'$set': {'age': 30}})

  # Insert if not exists 
  mgy.users.find_one_and_update({'_id': id}, {'$set': {'age': 30}}, upsert=True)
  # Delete Single Documents
  mgy.users.delete({
    'age': 22
  })

   # Delete all matching Documents
  mgy.users.delete({
    'age': 22
  }, all=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongoy-0.1.5.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

mongoy-0.1.5-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page