Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Mongodb basic Operation and Python connecting mongodb and basic Operation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the basic operation of Mongodb and Python connection mongodb and basic operation example analysis, the article introduces in great detail, has a certain reference value, interested friends must read it!

Mongodb is a nosql (non-relational) database based on distributed file storage

Although it is nosqldb, the documents in but mongodb can be relational

In mongodb, a table is a collection and the data in it is a document; a document is essentially a JSON data

Enter mongodb: mongo

Exit mongodb: exit

Library operation

Display library: show dbs

Select or create: use llsdb

# No matter whether the library exists or not, it will be entered by use. If the library does not exist, the library will not be created when the use does not store data and exit. So to create a library, use and then write the data.

View the library: db

Delete the library: db.dropDatabase (), you must first use into the library before deleting.

Set operation

Display collection: show collections

Create a collection: db.createCollection ('llscol' [, options])

Delete collection: db.llscol.drop ()

CURD of data

Insert data: db.llscol.insert ({name: 'lls', age: 18})

Insert multiple pieces of data:

Db.llscol.insert ([{name: 'lls1', age: 18}, {name:' lls2', age: 20}])

View data: db.llscol.find ()

Formatted data: db.llscol.find (). Pretty ()

Update data of full-text file: db.llscol.update ({name: ''}, {xx:' yy'})

# {name: 'hha'} is a condition for matching items to update.

Specify the field to update $set, {multi: true}: db.llscol.update ({name: 'lls1'}, {$set: {name:' xxx', age: 666}})

The {} before # is a condition, and only the first item that matches will be updated.

Update multiple {multi: true}: db.llscol.update ({name: 'lls1'}, {$set: {name:' lls666'}}, {multi: true})

Update the corresponding key-value pair in each JSON after the full-text file matches the corresponding item.

Delete data: db.llscol.remove ({name: 'lls1'})

# remove therefore contains the json of {name: 'lls1'}.

Delete only one {justOne: true}: db.llscol.remove ({name: 'lls1'}, {justOne: true})

# Delete the first item that matches.

Python operation mongodb

Import pymongoclient = pymongo.MongoClient ('192.168.0.104, 27017) # connect and establish client.db = client [' llsdb'] # select database_name db = client.llsdbstu = db ['student'] # select table_name stu = db.studentstu.insert_one ({' name': 'lls'}) stu.insert_many ([{' name1': 'lls1',' age': 18}, {'name2':' lls2', 'age': 20}) {'name3':' lls3', 'age': 30}]) stu.update_one ({' name1': 'lls1'}, {' $set': {'age': 20}}) stu.update_many ({' name2': 'lls2'}, {' $set': {'age': 90}}) print (stu.find_one ({' name2': 'lls2'})) # query the first line without args Or the first line matched. Content_find = stu.find ({}) # empty dictionary means to query all .print (content_find) for i in content_find: print (I) print (stu.delete_many ({'name':' lls'}). Deleted_count) print (stu.delete_many ({}). Deleted_count) "" square is as follows: insert_one, insert_many, update_one, update_many, delete_one, delete_many, find_one Find. The above is all the contents of this article entitled "sample Analysis of Mongodb basic Operation and Python connecting mongodb and basic Operation". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report