python - Return the count of a collection -
how can turn db.collection.count() python script?
i want return number.
#import json file mongodb logger.info(' import du fichier .json vers la base mongodb') #subprocess.call('mongoimport --db autoprivilege -c cars stockvo.json --jsonarray --upsert --drop',shell=true) p = subprocess.popen(['mongoimport', '--db', 'mybd', '-c', 'cars', '/opt/data/stockvo.json', '--jsonarray', '--upsert', '--drop'], stdout=subprocess.pipe, stderr=subprocess.pipe) stdout, stderr = p.communicate() if stdout: logger.info(stdout) if stderr: logger.error(stderr) thanks in advance.
you need connect database , run db.collection.count() count items in collection in python.
import pymongo # connect mongodb instance conn = pymongo.mongoclient('localhost') # connect database db = conn['mydb'] # print count of items in our collection print(db.collection.count())
Comments
Post a Comment