Skip to content

Instantly share code, notes, and snippets.

Avatar

Gyan Prakash Tripathi prakashthegyan

View GitHub Profile
View connect to mongodb
import pymongo
import pprint
mongo_uri = "mongodb://localhost:27017/"
client = pymongo.MongoClient(mongo_uri)
View find
table.find().sort("_id",pymongo.DESCENDING)
View store to file
df.to_csv('StructuredData.csv',index=False)
View storing to a dataframe
import pandas as pd
samples=table.find().sort("_id",pymongo.DESCENDING)[:100]
df=pd.DataFrame(samples)
df.head()
View insert in created
testInsert=mycoll.insert_one({"country":'India'}).inserted_id
client.list_database_names()
View create
mydb=client.testDB
mycoll=mydb.testColl
View delete many
myquery = ({ "name":{"$in": ["Gyan","Eliot"]}})
x = table.delete_many(myquery)
print(x.deleted_count, " documents deleted.")
View delete mike
table.delete_one({'name':'Mike'})
View mike
table.find_one({'name':'Mike'})
View find multiple
from bson.objectid import ObjectId
post_id=final.inserted_ids[0]
def parser(post_id):
document = table.find_one({'_id': ObjectId(post_id)})
return document
parser(post_id)