Skip to content

Instantly share code, notes, and snippets.

@atharvai
Last active July 29, 2020 16:22
Show Gist options
  • Save atharvai/0d9427e4738f2ec074a15fb6b83edffe to your computer and use it in GitHub Desktop.
Save atharvai/0d9427e4738f2ec074a15fb6b83edffe to your computer and use it in GitHub Desktop.
// list databases
db;
//select database to use
use test;
// find existing collections
db.getCollectionNames();
//insert 1 document into a collection that doesn't exist
db.mycoll.insertOne({"name":"atharva"});
//read all documents
db.mycoll.find({});
db.mycoll.updateOne({"name":"atharva"},
{"$set":{"team":"Data Engineering"}});
db.mycoll.find({});
db.mycoll.replaceOne({"name":"atharva"},
{"name":"Atharva Inamdar"});
db.mycoll.find({});
db.mycoll.insertMany([{"name":"Tinu"},{"name":"Steve"},{"name":"Anton"}]);
db.mycoll.find({});
//delete 1 doc by id
db.mycoll.deleteOne({"_id":ObjectId("5f0978f5c9f1ef571c9577f2")});
db.mycoll.find({});
//delete all documents
db.mycoll.deleteMany({});
import pymongo
from bson import ObjectId
client = pymongo.MongoClient('localhost', 27017)
if __name__ == '__main__':
print(client.list_database_names())
test_db = client.get_database('test')
print(test_db.list_collection_names())
coll = test_db['pycoll']
coll_cur = coll.find({})
for doc in coll_cur:
print(doc)
# insert one document
coll.insert_one({'name': 'atharva'})
coll_cur = coll.find({})
for doc in coll_cur:
print(doc)
coll.update_one({'_id':ObjectId('5f097f896bddf1691d73b9e1')},
{'$set':{'team': "Data Eng"}})
print('update document:')
coll_cur = coll.find({})
for doc in coll_cur:
print(doc)
coll.delete_many({})
print('delete all:')
coll_cur = coll.find({})
for doc in coll_cur:
print(doc)
#!/usr/bin/env bash
# if you don’t have IDE:
docker run --name mongo-demo -d --rm mongo:4.2
docker exec -it mongo-demo bash
# To Exit:
quit();
exit
#stop the container:
docker stop mongo-demo
#If you have IDE:
docker run –p 27017:27017 --name mongo-demo --rm -it mongo:4.2
# To Exit: Ctrl+C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment