Friday, April 1, 2016

pymongo : python working with mongodb

https://api.mongodb.org/python/current/tutorial.html
https://gist.github.com/vgoklani/1556251
https://api.mongodb.org/python/current/api/pymongo/database.html
https://docs.mongodb.org/getting-started/python/query/
https://docs.mongodb.org/manual/reference/command/delete/
https://docs.mongodb.org/manual/tutorial/remove-documents/
https://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

# Mongodb backup/restore: using mongoexport and mongoimport
http://www.mkyong.com/mongodb/mongodb-import-and-export-example/
http://www.tothenew.com/blog/export-very-large-data-from-a-mongodb-collection-using-script/
http://stackoverflow.com/questions/11255630/how-to-export-all-collection-in-mongodb
https://docs.mongodb.com/v3.0/reference/program/mongorestore/
http://www.tutorialspoint.com/mongodb/mongodb_create_database.htm


# Note: Jinja {% if x != "test" and x != "local" %}    : jinja code in html comment is not allowed:
Do not put html comment for jinja code such as <!-- {{local_dat}} -->

# export: mongoexport --db test --collection collections --out db_bak.1
# import: mongoimport --db test --collection collections --file db_bak.1

# Also: mongodump/mongorestore

mongodump -d <our database name> -o <directory_backup>
And to "restore/import" that, i used (from directory_backup/dump/):

mongorestore <our database name>

# backup/restore complete complete server instance
just use: mongodump to backup & mongorestore to restore the complete instance. No need to stop the mongo service: The backup command creates a dump folder in the directory where mongodump is executed. While restore, the mongorestore looks for dump folder in the directory where the mongorestore command is executed.


# To create a database in mongodb , at least one document should be created. 

#!/bin/bash

backupDir=/data/db-backup/
dbName='commit_db'
collectionName='commits'
numDays=7

curDate=$(date +'%Y-%m-%d')
curLog="${backupDir}/${curDate}.bak"

echo "Removing backups older than ${numDays} days"
find ${backupDir} -type f -name "*.bak" -delete

echo "Exporting data into ${curLog}"
/usr/bin/mongoexport --verbose --db ${dbName} --collection ${collectionName} --out ${curLog}



# default used db is: test or last active database if removed or used

import pymongo
connection = pymongo.MongoClient() # Create connection to database
db = connect.<db_name>  # same as mongo console : 'use'

MongoClient(host='localhost'port=27017document_class=dicttz_aware=Falseconnect=True**kwargs)

collection = db.test_collection
document = collection.test_document

# Other usage of the pymongo methods

# default connection to localhost:27017 : MongoClient()
MongoClient('localhost', 27017)
MongoClient('mongodb://localhost:27017/') 

db = connection['hello_boy']
db = connect.hello_boy
collection = db.test_collection
collection = db['test-collection']

# Note: Console commands
1. default database variable on mongo console: 'db'. This is initialized by: 'use'
2. drop database: db.dropDatabase()
3. db # displays current database
4. show dbs
5. show collections
# Write Data:
use test
collection = db.collection
db.collection.insert( { item: "card", qty: 15 } )

# Read Data
use test
db.collection.find()

# Python methods:
 connection.drop_database('hellodb') : connection.drop_database(<databse_name>)
connection['mydatabase'].drop_collection('mycollection')
dababase_name = db.name : name from the db object









c.database_names() # list all databases :: "show dbs"
# drop collection :: "db.collection.drop()"  
db.collection_names() # list all collections in the selected database
 

db.inventory.remove( { type : "food" } ) # To delete data
db.inventory.remove({}) # Removes all data from the collection

# Python equivalent of remove: delete_many(search data)

db.inventory.update(
  { item: "ABC1" },
  { $set: { "details.model": "14Q2" } }
)

# Python read/write data

#!/usr/bin/env python
import shlex
import subprocess
import os
from collections import OrderedDict

import pymongo

# command run
def runcmd(cmd):
cmd = shlex.split(cmd)
try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, error = proc.communicate()
return out + error
except:
print 'Invalid Command: ' + str(cmd)
return -1

# multiple commands in a process
def runcmds(cmds_list):
bash_path = runcmd('which bash').strip() # strip() used to trim line feed
p = os.popen('%s' % bash_path, 'w')
for cmd in cmds_list:
p.write('%s\n' % cmd)
status = p.close()

if status is None:
print 'Commands executed OK'
else:
print 'One or more command failed to run'

return status

def db_init():
conn = pymongo.MongoClient('localhost', 27017)
dbh = conn.test
collection = dbh.collections
return collection

def db_insert(collection, dataItem):
#print json.dumps(dataItem, indent=4, sort_keys=True)
print 'OK...'
try:
commit_id = collection.insert_one(dataItem).inserted_id
print "Inserted: record with id:{0}".format(dataItem['_id']) # _id is added to the data item by insert_one
except pymongo.errors.DuplicateKeyError:
print "Error: record with id:{0} already exists".format(dataItem['_id'])


# insert all values as string and process the number at read time
def set_var(field_names, values, num=False):
fields = field_names.strip().replace(' ', '').split(',')
values = values.strip().replace(' ', '').split(',')
if num:
values = map(int, values)
data = zip(fields, values)
#print data
#print dict(data)
collection = db_init()
db_insert(collection, dict(data))

def get_var(conditions_list=None):
field_names = []
values = []
object_ids = []
collection = db_init()
for i in collection.find():
field_group = []
values_group = []
for key, value in i.iteritems():
if key != '_id':
field_group.append(str(key))
values_group.append(str(value))
else:
object_ids.append(str(value))
field_names.append(field_group)
values.append(values_group)

print field_names
print values
print object_ids
def list_vars():
pass

def clean_vars():
pass


if __name__ == '__main__':
#l = ['mongo', 'show dbs']
#print runcmds(l)
#set_var('name, age, size', 'As, 12, 13')
get_var()
 








No comments:

Post a Comment