mongodb基本語法

2021-09-25 19:06:31 字數 1649 閱讀 7083

建立資料庫:

use test
展示資料庫

show dbs
剛建立的資料庫 test並不在資料庫的列表中, 要顯示它,我們需要向 runoob 資料庫插入一些資料。

db.runoob.insert()
刪除資料庫

首先切換到需要刪除的資料庫

use test    //存在test則切換到test資料庫,不存在則建立test資料庫
然後執行刪除資料庫命令

db.dropdatabase()
建立集合(相當於資料庫中的表)

db.createcollection("test1")
檢視集合

show tables
刪除集合

db.test1.drop()
插入文件(資料庫中的資料)

db.test1.insert()
修改.更新文件

db.col.update(,})
刪除文件

db.test1.remove({})  //刪除所有資料

db.test1.remove() //刪除查詢到的所有資料

db.test1.remove(,1) //只刪除查詢到的第一條資料

查詢文件

db.test1.find()
如果你需要以易讀的方式來讀取資料,可以使用 pretty() 方法,語法格式如下:

db.test1.find().pretty()
mongodb 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,即常規 sql 的 and 條件。

db.test1.find().pretty()

db.col.find().pretty()

查詢鍵 by 值為 菜鳥教程 或鍵 title 值為 mongodb 教程 的文件。

db.col.find(,]}).pretty()
and 和 or 聯合使用,類似常規 sql 語句為: 『where likes>50 and (by = 『菜鳥教程』 or title = 『mongodb 教程』)』

db.col.find(, $or: [,]}).pretty()
操作符

(>) 大於 -$gt

(<) 小於 - $lt

(>=) 大於等於 - $gte

(<= ) 小於等於 - $lte

(!= ) 不等於 - $ne

MongoDB基本語法

sql術語 概念 mongodb術語 概念 解釋 說明 database database 資料庫table collection 資料表 集合 rowdocument 資料記錄行 文件 column field 資料字段 域 index index 索引table joins 表連線,mongod...

mongodb的基本語法

1 啟動shell 主要用crt 軟體的時候終端要選擇linux,否則不能退格鍵有時候出問題 root saltstack mongodb mongo show dbs 檢視資料庫 admin empty local 0.078gb modbtest 0.078gb myinfo empty tes...

mongodb的基本語法

推薦 增加一條資料 db.user.insertone 增加多條資料 db.user.insertmany 查詢所有 db.user.find 查詢符合條件的第一條資料 db.user.findone 查詢年齡大於125歲的人 db.user.find 查詢年齡小於300歲的人 db.user.fi...