Get the App
SLTechnology News&Howtos  ›  Database  › 

Basic syntax of mongodb

Shulou Source: shulou.com Published: 2022-06-01 13:04:26 10月30日 Update

1. Start shell: (the terminal should select linux when mainly using crt software, otherwise the Backspace key can sometimes go wrong.)

[root@saltstack mongodb] # mongo

> show dbs # View the database

Admin (empty)

Local 0.078GB

Modbtest 0.078GB

Myinfo (empty)

Test (empty)

Xiaoluo 0.078GB

> use dbtest # uses a database. If not, it will be created automatically.

Switched to db dbtest

# create a dictionary document, which is stored in key,value:

> test = {'id':1,'name':'xiaoming','job':'it'}

{"id": 1, "name": "xiaoming", "job": "it"}

> test

{"id": 1, "name": "xiaoming", "job": "it"}

# automatically create a table called dbtest, and then insert the dictionary test into it:

> db.dbtest.insert (test)

WriteResult ({"nInserted": 1})

# or you can insert data manually:

> db.dbtest.insert ({'id':2,'name':'xiaoli','job':'it'})

WriteResult ({"nInserted": 1})

# how to use find to view data:

> db.dbtest.find ()

{"_ id": ObjectId ("5513375e83aef55e0cc2d05c"), "id": 1, "name": "xiaoming", "job": "it"}

{"_ id": ObjectId ("5513379383aef55e0cc2d05d"), "id": 2, "name": "xiaoli", "job": "it"}

> show tables

Dbtest

System.indexes

# find according to the condition

> db.dbtest.find ({'id':1})

{"_ id": ObjectId ("5513375e83aef55e0cc2d05c"), "id": 1, "name": "xiaoming", "job": "it"}

# update the selection according to the conditions

> db.dbtest.update ({'id':2}, {' id':2,'name':'xiaoluo','job':'it'})

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

> db.dbtest.find ({'id':2})

{"_ id": ObjectId ("5513379383aef55e0cc2d05d"), "id": 2, "name": "xiaoluo", "job": "it"}

>

# use the remove method to delete according to conditions. Delete id=2 's dictionary:

> db.dbtest.remove ({'id':2})

WriteResult ({"nRemoved": 1})

> db.dbtest.find ()

{"_ id": ObjectId ("5513375e83aef55e0cc2d05c"), "id": 1, "name": "xiaoming", "job": "it"}

Tags: Data dictionary condition database method manual document method time sometimes terminal software problem storage update selection syntax Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Tech Info MySQL Microsoft