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

How to use MongoDB Database

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces "the use of MongoDB database". In the daily operation, I believe that many people have doubts about the use of MongoDB database. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of MongoDB database. Next, please follow the editor to study!

MongoDB

MongoDB is a product between relational database and non-relational database, which is the most functional and most like relational database among non-relational databases.

Ubuntu installs MongoDB. Direct sudo apt-get install mongodbMongoDB is a product between relational database and non-relational database, which is the most functional and most like relational database among non-relational databases. It supports a very loose data structure, which is similar to json's bson format, so it can store more complex data types.

The most important feature of Mongo is that the query language it supports is very powerful, and its syntax is somewhat similar to the object-oriented query language. It can almost achieve most of the functions similar to the single table query of relational database, and also supports the establishment of data indexing.

Store the data as a document that is similar to the Json format, such as the following.

{name: "gross margin", age:18, address: {city: "Dongguan", country: "china"}}

MongoDB data model

"how to enter and exit mongo"

Library-level operation statement

Show all libraries: show dbs

Switch / create database: use database name

View the library: db

Delete library: db.dropDatabase ()

Set operation statement

Displays the collection of the current database: show collections

Create a collection: db.createCollection (name)

Delete the collection: db. Collection name .drop ()

Document operation

Add document (data)

Db. Collection name .insert (document)

Each piece of data, is a document, is a json example: db.student.insert ({name:' gross margin', age:18})

Note:

When adding a document, if you do not specify the _ id parameter MongoDB, the document will be assigned a unique ObjectId

Given _ id example: db.student.insert ({'_ id':1, name:' gross margin', age:18})

Add multiple documents

Db.student.insert ([{name:' Maori, sex:' male', age:18}, {name:' Maori's father', sex:' male', age:47}, {name:' Maori's sister', sex:' female', age:23}, {name:' Maori's mother', sex:' female', age:44},])

Query documents (data)

Db. Collection name. Find ([conditions])

View all the data in the collection: db.student.find ()

Formatted display: db.student.find (). Pretty ()

View data that meet the criteria: db.student.find ({name:' gross margin'})

Conditional query

And condition {$and: [{expression1}, {expression1},...]}

Or condition {$or: [{expression1}, {expression1},...]

} db.student.find ({$or: [{$and: [{sex:' female'}, {age:23}]}, {$and: [{sex:' male'}, {age: {$gte:18}]}]})

Modify document (data)

Db. Collection name .update (, {multi:})

Modify a piece of data: db.student.update ({sex:' male'}, {age:20}) changes the age of the male in the table to 20

Specify attribute modification: {$set: {age:20}} db.student.update ({name:' gross margin'}, {$set: {age:666, sex: 'don't tell you'}})

Change the age of Maori to 666 and sex will not tell you.

Update all documents in the collection that meet the criteria: {multi: true}

Db.student.update ({sex:' male'}, {$set: {sex:' female'}}, {multi:true})

Change all the buttons to women.

Delete document (data)

Db. Collection name .remove (, {justOne:})

Delete all documents in the collection: db.student.remove ({}) Delete all documents in the collection that meet the criteria db.student.remove ({sex: 'male'}) delete only the first document in the collection that meets the criteria: {justOne: true}

Db.student.remove ({sex:' male'}, {justOne:true})

Operate in Python program

For MongoDB to operate MongoDB in Python, you need to use the PyMongo library, execute the following command to install: pip3 install pymongo.

You need to use the MongoClient module in the PyMongo library to connect. There are two ways to create a connection. By default, you only need to pass in IP and port number. If there is an account password in the database, you need to specify the database to connect to. The public network IP of MongoDB must be open before the connection can be successful.

> from pymongo import MongoClient > client = MongoClient ('mongodb://192.168.92.92:27017') > db = client.school > for student in db.students.find ():. Print ('name:', student ['name']) at this point, the study on "how to use MongoDB database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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