1024programmer Java Add, delete, modify and query pymongo operations

Add, delete, modify and query pymongo operations

Module import

from pymongo import MongoClient

Instantiate client and establish connection

client=MongoClient(host="127.0.0.1",port=27017)
 collection=client["test"]["std  "] #test is the database, std is the collection

Insert a piece of data

collection.insert({"name":"xiaoming","age":18})

Insert a piece of data and modify the default _id value.

collection.insert({"_id":20181126,"name":"xiaoming","age":18})

Insert a piece of data, use ret to receive it, and print out the value of _id

ret=collection.insert({"_id":20181126,"name":"xiaoming","age":18}) print(ret) #Run result: 20181126

Insert multiple pieces of data insert_many(),

data_list=[{"name":"test{}  ".format(i)} for i in range(10)]
 collection.insert_many(data_list)

Query a record find_one()

t=collection.find_one({"name":"xiaowang"})
 print(t)

To query multiple records find(), the output is a cursor.

t=collection.find({"name":"xiaowang"})
 print(t)

Update a piece of data update_one()

collection.update_one({"name":"xiaoming"},{"$set":{"name":"xiaozhang"}})

Update multiple pieces of data update_many()

collection.update_many({"name":"xiaoming"},{"$set":{"name":"xiaozhang"}})

delete_one() to delete a piece of data

collection.delete_one({"name":"xiaowang"})

Delete multiple pieces of data delete_many()

collection.delete_many({"name":"xiaowang"})

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/add-delete-modify-and-query-pymongo-operations/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索