Python MongoDB Insert Document

document in MongoDB is the same as a record in SQL databases.

MongoDB-д баримт бичиг нь SQL өгөгдлийн сангийн мөртэй адилхан.

Insert Into Collection – Цуглуулга руу өгөгдөл оруулах #

To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method.

Мөр оруулах, эсвэл баримт бичгийг оруулахыг MongoDB-д цуглуулга оруулах гэж нэрлэдэг, цуглуулга оруулахыг тулд insert_one() аргыг ашигладаг.

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_one() аргументын анхны параметр нь оруулах гэж буй баримт бичигт багтах бүх талбарын нэр болон утгыг агуулсан толь бичиг (dictionary) юм.

Example – Жишээ #

Insert a record in the “customers” collection:

“customers” цуглуулгад өгөгдөл оруулах:

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)

Return the _id Field – _id талбарыг буцаах #

The insert_one() method returns a InsertOneResult object, which has a property, inserted_id, that holds the id of the inserted document.

insert_one() арга нь оруулсан баримт бичгийн id-г хадгалсан InsertOneResult объектыг буцаана. Энэ объект нь inserted_id шинж чанарыг агуулдаг.

Example – Жишээ #

Insert another record in the “customers” collection, and return the value of the _id field:

“customers” цуглуулгад өөр нэг бүртгэл оруулаад, _id талбартын утгыг буцаа:

mydict = { "name""Peter""address""Lowstreet 27" }
x = mycol.insert_one(mydict)
print(x.inserted_id)

If you do not specify an _id field, then MongoDB will add one for you and assign a unique id for each document.

Хэрэв та _id талбарыг тодорхойлоогүй бол MongoDB таны өмнөөс үүнийг нэмж, баримт бүрт өвөрмөц ID олгоно.

In the example above no _id field was specified, so MongoDB assigned a unique _id for the record (document).

Дээрх жишээнд _id талбар тодорхойлогдоогүй тул MongoDB нь өгөгдөлд (баримт бичиг)-т өвөрмөц _id-г өгсөн.

Insert Multiple Documents – Олон Баримт бичгүүд нэмэх  #

To insert multiple documents into a collection in MongoDB, we use the insert_many() method.

MongoDB цуглуулгад олон баримт бичгийг оруулахын тулд insert_many() аргыг ашигладаг.

The first parameter of the insert_many() method is a list containing dictionaries with the data you want to insert:

insert_many() аргын эхний параметр нь оруулах өгөгдөл бүхий толь бичгүүдийг агуулсан жагсаалт юм.

Example #

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mylist = [
  { "name": "Amy", "address": "Apple st 652"},
  { "name": "Hannah", "address": "Mountain 21"},
  { "name": "Michael", "address": "Valley 345"},
  { "name": "Sandy", "address": "Ocean blvd 2"},
  { "name": "Betty", "address": "Green Grass 1"},
  { "name": "Richard", "address": "Sky st 331"},
  { "name": "Susan", "address": "One way 98"},
  { "name": "Vicky", "address": "Yellow Garden 2"},
  { "name": "Ben", "address": "Park Lane 38"},
  { "name": "William", "address": "Central st 954"},
  { "name": "Chuck", "address": "Main Road 989"},
  { "name": "Viola", "address": "Sideway 1633"}
]
x = mycol.insert_many(mylist)
#print list of the _id values of the inserted documents:
print(x.inserted_ids)

The insert_many() method returns a InsertManyResult object, which has a property, inserted_ids, that holds the ids of the inserted documents.

insert_many() арга нь оруулсан баримтуудын ID-ийг агуулсан inserted_ids шинж чанартай InsertManyResult объектыг буцаана.

Insert Multiple Documents, with Specified IDs – Олон баримт бичгийг тодорхойлсон ID-уудтайгаар оруулах #

If you do not want MongoDB to assign unique ids for your document, you can specify the _id field when you insert the document(s).

Хэрэв та MongoDB-д өөрийн баримт бичигт өвөрмөц ID оноохыг хүсэхгүй байгаа бол баримт бичгийг оруулахдаа _id талбарыг зааж өгч болно.

 

Remember that the values has to be unique. Two documents cannot have the same _id.

Санах хэрэгтэй зүйл бол _id талбарын утга нь өвөрмөц байх ёстой. Хоёр баримт бичигт ижил _id байх боломжгүй.

Example – Жишээ #

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mylist = [
 { "_id"1"name""John""address""Highway 37"},
  { "_id"2"name""Peter""address""Lowstreet 27"},
  { "_id"3"name""Amy""address""Apple st 652"},
  { "_id"4"name""Hannah""address""Mountain 21"},
  { "_id"5"name""Michael""address""Valley 345"},
  { "_id"6"name""Sandy""address""Ocean blvd 2"},
  { "_id"7"name""Betty""address""Green Grass 1"},
  { "_id"8"name""Richard""address""Sky st 331"},
  { "_id"9"name""Susan""address""One way 98"},
  { "_id"10"name""Vicky""address""Yellow Garden 2"},
  { "_id"11"name""Ben""address""Park Lane 38"},
  { "_id"12"name""William""address""Central st 954"},
  { "_id"13"name""Chuck""address""Main Road 989"},
  { "_id"14"name""Viola""address""Sideway 1633"}
]
x = mycol.insert_many(mylist)
#print list of the _id values of the inserted documents:
print(x.inserted_ids)

Powered by BetterDocs

Leave a Reply