In [19]:
import pymongo
from pymongo import MongoClient

import getpass

import base64

connecting to the database


In [36]:
client = MongoClient('localhost:27017')
db = client.arXivDB
db.users.count()


Out[36]:
0

In [49]:
while True:
    user = {}
    user['username'] = input('choose username: ').lower()
    user['pin'] = base64.b64encode(bytes(str(getpass.getpass('choose pin: ')), encoding="UTF-8"))
    if db.users.find({'username': user['username']}).count():
        print('the username already exists, try a new one')
    else:
        db.users.insert_one(user)
        username = None
        pin = None
        break
print('the total number of users is: {}'.format(db.users.count()))


choose username: adrian
choose pin: ········
the total number of users is: 9

In [44]:
list(db.users.find({}))


Out[44]:
[{'_id': ObjectId('581329755284fc092c69c1ab'),
  'pin': b'MTIzNA==',
  'username': 'amir'},
 {'_id': ObjectId('581350715284fc092c69c1ac'),
  'pin': b'MTIzNA==',
  'username': 'gil'},
 {'_id': ObjectId('5813507a5284fc092c69c1ad'),
  'pin': b'MTIzNA==',
  'username': 'william'},
 {'_id': ObjectId('5813508d5284fc092c69c1ae'),
  'pin': b'MTIzNA==',
  'username': 'kris'}]

In [ ]: