時(shí)間:2024-02-09 10:40作者:下載吧人氣:16
mongodb的基礎(chǔ)概念介紹
database #數(shù)據(jù)庫
collection #集合,類似于mysql中的表
filed #類似于mysql中字段
document #每行的記錄
mongo客戶端的命令自動(dòng)提示功能
使用tab鍵
查詢所有的庫,默認(rèn)自帶三個(gè)庫
show dbs;
show databases;
show dbs; admin 0.000GB config 0.000GB local 0.000GB show databases; admin 0.000GB config 0.000GB local 0.000GB
關(guān)閉數(shù)據(jù)庫
mongo客戶端提供一個(gè)正確關(guān)閉mongodb服務(wù)器的方法
use admin
db.shutdownServer()
mongodb創(chuàng)建庫、創(chuàng)建集合、插入數(shù)據(jù)(key value的字典方式插入)
use wygzs #use即可,無需顯式創(chuàng)建數(shù)據(jù)庫
db.mydata.insert({name:’ymz’, age: 22}) #無需顯示創(chuàng)建collection。name、age為filed,shijiange1、28為值
db.mydata.insert({name:’ymz’, age: 22}) WriteResult({ “nInserted” : 1 })
show dbs;
show dbs; admin 0.000GB config 0.000GB local 0.000GB wygzs 0.000GB
show collections; ## 顯示表
show collections mydata
show tables; ## 顯示表,類似mysql
show tables mydata
db.myuser.insert( {‘name’: ‘shijiange2’, age: 26} )
db.mydata.insert({name:’ymz’, age: 27}) WriteResult({ “nInserted” : 1 })
db.myuser.insert( {‘name’: ‘shijiange3’, age: 27} )
db.mydata.insert({name:’ymz’, age: 27}) WriteResult({ “nInserted” : 1 })
查詢集合數(shù)據(jù),默認(rèn)有個(gè)_id
use wygzs
db.mydata.find() #查詢所有數(shù)據(jù)
db.mydata.find() { “_id” : ObjectId(“63622f873282610e103913fa”), “name” : “ymz”, “age” : 22 } { “_id” : ObjectId(“63622fdd3282610e103913fb”), “name” : “ymz”, “age” : 23 } { “_id” : ObjectId(“63622fdf3282610e103913fc”), “name” : “ymz”, “age” : 24 }
db.mydata.find({‘age’: 22})
db.mydata.find({‘age’: 22}) { “_id” : ObjectId(“63622f873282610e103913fa”), “name” : “ymz”, “age” : 22 }
刪除集合數(shù)據(jù)
use wygzs;
db.mydata.remove({ age: 22′ }) #有條件的刪除
db.mydata.remove({‘age’:22}) WriteResult({ “nRemoved” : 1 })
db.mydata.remove( {} ) #刪除數(shù)據(jù)
db.mydata.remove({}) WriteResult({ “nRemoved” : 2 })
db.myuser.drop() #刪除集合
db.mydata.drop() true
show dbs; # 如果沒有集合了,數(shù)據(jù)庫也就沒有了
show dbs; admin 0.000GB config 0.000GB local 0.000GB
集合的field不用固定,一般來說不這樣子使用
db.myuser.insert( {age: 28} )
db.myuser.insert( {age: 28} ) WriteResult({ “nInserted” : 1 })
db.myuser.insert( {‘location’: ‘hangzhou’} )
db.myuser.insert( {‘location’: ‘hangzhou’} ) WriteResult({ “nInserted” : 1 })
db.myuser.find()
db.myuser.find() { “_id” : ObjectId(“636233c595502fbd56836b94”), “age” : 28 } { “_id” : ObjectId(“636233cf95502fbd56836b95”), “location” : “hangzhou” }
更新集合數(shù)據(jù)
use wygzs;
db.myuser.update({ ‘location’: ‘hangzhou’ }, { $set: { ‘location’: ‘shanghai’ } })
db.myuser.update({ ‘location’: ‘hangzhou’ }, { $set: { ‘location’: ‘shanghai’ } }) WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.myuser.update({ age: 28 }, {$set: { age: 30 }})
db.myuser.update({ age: 28 }, {$set: { age: 30 }}) WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.myuser.find()
db.myuser.find() { “_id” : ObjectId(“636233c595502fbd56836b94”), “age” : 30 } { “_id” : ObjectId(“636233cf95502fbd56836b95”), “location” : “shanghai” }
刪除數(shù)據(jù)庫
use wygzs
db.dropDatabase()
db.dropDatabase() { “ok” : 1 }
注意:
mongodb自帶的三個(gè)庫不要?jiǎng)?/p>
網(wǎng)友評論