Get the App
SLTechnology News&Howtos  ›  Database  › 

Installation and use of typical NoSQL database-- MongoDB installation and use

Shulou Source: shulou.com Published: 2022-06-01 10:14:55 09月28日 Update

Installation mode

Use the apt-get install mongodb command directly, or install it with the source package.

Hadoop@dblab:/$ sudo apt-get update

Hadoop@dblab:/$ sudo apt-get install-y mongodb-org

Hadoop@dblab:/$ mongo-version

MongoDB shell version: 3.2.22

Hadoop@dblab:/$ sudo service mongodb start # launch MongoDB

Hadoop@dblab:/$ mongo # enters MongoDB Shell mode

> use school # switch to shcool database and create it automatically when you use it

Switched to db school

> db.createCollection ('teacher') # create a collection

{"ok": 1}

> show dbs # displays a list of databases

Local 0.000GB

School 0.000GB

> db.student.insert ({_ id:1,sname:'zhangsan',sage:20}) # insert data

WriteResult ({"nInserted": 1})

> db.student.insert ({_ id:2,sname:'lisi',sage:22}) # insert data

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

> use school

Switched to db school

> show collections # displays the collection of the current database

Student

Teacher

# find data

> db.student.find () # find all records

{"_ id": 1, "sname": "lisi", "sage": 22}

{"_ id": 2, "sname": "lisi", "sage": 22}

> db.student.remove ({_ id: 2}) # Delete data

WriteResult ({"nRemoved": 1})

> db.student.find ()

{"_ id": 1, "sname": "lisi", "sage": 22}

> db.student.insert ({_ id:2,sname:'zhangsan',sage:25})

WriteResult ({"nInserted": 1})

> db.student.find ()

{"_ id": 1, "sname": "lisi", "sage": 22}

{"_ id": 2, "sname": "zhangsan", "sage": 25}

>

# modify data

> db.student.update ({_ id:2}, {$set: {sage:88}}, false,true)

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

> db.student.find () .pretty ()

{"_ id": 1, "sname": "lisi", "sage": 22}

{"_ id": 2, "sname": "zhangsan", "sage": 88}

# Delete data

> db.student.remove ({sname:'lisi'})

WriteResult ({"nRemoved": 1})

# deleting a collection

> db.student.drop ()

> show collections

Teacher

> exit # exits MongoDB Shell mode

Bye

Tags: Data database schema command mode source code switching typical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Linux Shulou Technology Redmi