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

Basic operating commands commonly used in MongoDB

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly explains the "MongoDB commonly used basic operating commands", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "MongoDB commonly used basic operating commands" bar!

There is the concept of a database in the MongoDB database, but there is no schema (all the information is saved according to the document). The structure of the document is the JSON structure, but some of MongoDB's own operators are used in some data processing.

1. Switch to stone database:

> db

Test

Execute the "db" command to display the current database object

> use stone

Switched to db stone

> show databases

Admin 0.000GB

Local 0.000GB

In fact, the database will not be created at this time, and the database can only be created after the collection data is saved in the database.

● admin: from a permission perspective, this is the "root" database. If you add a user to this database, the user automatically inherits the permissions of all databases. Some specific server-side commands can only be run from this database, such as listing all databases or shutting down the server.

● local: this data is never replicated and can be used to store any collection limited to a single local server

● config: when Mongo is used for sharding settings, the config database is used internally to store information about sharding.

2. Create a collection:

> db.createCollection ("emp")

{"ok": 1}

> show databases

Admin 0.000GB

Local 0.000GB

Stone 0.000GB

Only at this time will the stone database really exist.

3. But very often, if you follow the above code form, you will feel abnormal, because normal people use MongoDB database collection operation to save a data directly to it.

> db.dept.insert ({"deptno": 10, "dname": "Finance Department", "loc": "Beijing"})

WriteResult ({"nInserted": 1})

4. View all collections

> show collections

Dept

Emp

Found that the dept collection is created automatically.

5. View the data of the emp table

Grammar: db. Collection name .find ({several conditions})

> db.dept.find ()

{"_ id": ObjectId ("59904d44d31a95e93db0da1c"), "deptno": 10, "dname": "Finance Department", "loc": "Beijing"}

From the point of view of the traditional data table (the collection is equivalent to the structure of the table), once the structure of the table is defined, the content must be written in accordance with the requirements of its definition. But MongoDB is different, it can expand the data at will.

6. Add irregular data

> var deptData= {

... "deptno": 20

... "dname": "R & D"

... "loc": "Shenzhen"

... "count": 20

... "avg": 8000

...}

> db.dept.insert (deptData)

WriteResult ({"nInserted": 1})

> db.dept.find ()

{"_ id": ObjectId ("59904d44d31a95e93db0da1c"), "deptno": 10, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("59904f2dd31a95e93db0da1d"), "deptno": 20, "dname": "R & D Department", "loc": "Shenzhen", "count": 20, "avg": 8000}

At this time, the content of the dept collection can be freely defined by the user, without considering other structures, so it must be made clear that there is absolutely no operation to view the collection structure in the MongoDB database.

7. Questions about ID

Each row of records in the MongoDB collection will automatically generate a "_ id": ObjectId ("59904f2dd31a95e93db0da1d") data, which consists of "timestamp + machine code + process PID + counter". The ID information is generated for the user by the MongoDB database itself.

8. View a single document information

> db.dept.findOne ()

{

"_ id": ObjectId ("59904d44d31a95e93db0da1c")

"deptno": 10

"dname": "Finance Department"

"loc": "Beijing"

}

9. Delete data

> db.dept.remove ({"_ id": ObjectId ("59904d44d31a95e93db0da1c")})

WriteResult ({"nRemoved": 1})

> db.dept.find ()

{"_ id": ObjectId ("59904f2dd31a95e93db0da1d"), "deptno": 20, "dname": "R & D Department", "loc": "Shenzhen", "count": 20, "avg": 8000}

10. Update data

> var deptData= {

... "deptno": 30

... "dname": "IT"

... "loc": "Beijing"

...}

> db.dept.update ({"_ id": ObjectId ("59904f2dd31a95e93db0da1d")}, deptData)

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.dept.find ()

{"_ id": ObjectId ("59904f2dd31a95e93db0da1d"), "deptno": 30, "dname": "IT", "loc": "Beijing"}

11. Delete the collection

Grammar: db. Collection name .drop ()

> db.dept.drop ()

True

> show collections

Emp

12. Delete the database (delete the current database)

> db.dropDatabase ()

{"dropped": "stone", "ok": 1}

> show dbs

Admin 0.000GB

Local 0.000GB

To delete a database is to delete the current database, which must be switched to the database before it can be deleted.

Thank you for your reading, these are the contents of the "basic operating commands commonly used in MongoDB". After the study of this article, I believe you have a deeper understanding of the basic operating commands commonly used in MongoDB, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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