M ONGODB INESRT Insert Into Collection A document in MongoDB is the same as a record in SQL databases. To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method. The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert. Insert a record in the "customers" collection: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydict = { "name": "John", "address": "Highway 37" } x = mycol.insert_one(mydict) #The insert_one() method returns a InsertOneResult object print(x.inserted_id) #inserted_id, that holds the id of the inserted document. Insert Multiple Documents To insert multiple documents into a collection in MongoDB, we use the insert_many() method. The first param...
FOR BEGINNER PROGRAMMER & COMPUTER SCIENCE STUDENTS