mongo命令支持JS語句
show dbs 列出服務(wù)器中的所有數(shù)據(jù)庫
數(shù)據(jù)庫中必須有內(nèi)容
show collections 列出當(dāng)前數(shù)據(jù)庫中的所有集合
db 顯示當(dāng)前數(shù)據(jù)庫
use <數(shù)據(jù)庫名> 切換數(shù)據(jù)庫
如果數(shù)據(jù)庫不存在,則創(chuàng)建數(shù)據(jù)庫
db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫
db.collection.insert({}) 創(chuàng)建一個集合并插入數(shù)據(jù)
將同類數(shù)據(jù)視為一個集合
集合中數(shù)據(jù)具體相似的屬性
集合是一個容器,內(nèi)部可放置N多數(shù)據(jù)
可以對這些數(shù)據(jù)進行篩選、排序
db.collection.find() 列出集合中的數(shù)據(jù)
顯示更多數(shù)據(jù): it
db.collection.drop() 刪除集合
db.collection.find().sort({屬性:1/-1}) 按指定的屬性排序
1是正序,-1:倒序
db.collection.find().limit(n) 顯示前n條數(shù)據(jù)
db.collection.find().skip(n) 跳過前n條數(shù)據(jù)
條件查找
db.collection.find({屬性:value}) 屬性==值
db.collection.find({屬性:{運算符:value}})
運算符:
$lt less than 小于
$lte less than equal 小于等于
$gt greater than 大于
$gte greater than equal 大于等于
$ne not equal 不等于
對同一屬性使用兩個運算符
db.collection.find({ 屬性: { 運算符1:value, 運算符2:value } })
如:db.students.find({ age: { $gt: 6, $lt: 16 } })
對不同屬性同時限定條件(兩個條件同時成立)
db.collection.find( { 屬性1: { 運算符:value }, 屬性2: { 運算符:value } })
或條件
db.collection.find({
$or: [
{屬性: value},
{屬性: value},
{屬性: value}
]
})
使用函數(shù)篩選數(shù)據(jù)
db.collection.find({
$where: function(){
return true/false
}
})
更新數(shù)據(jù)
db.collection.update(
{ 數(shù)據(jù)篩選條件 },
{ $set: {新數(shù)據(jù)} },
{
multi: true/false 是否更新多條數(shù)據(jù),默認false
upsert: true/false 如果數(shù)據(jù)不存在是否插新數(shù)據(jù),默認false
}
)
刪除數(shù)據(jù)
db.collection.remove(
{ 數(shù)據(jù)篩選條件 },
true/false 默認true,刪除所有符合條件的數(shù)據(jù),false只刪除1條
}