mongo shell命令

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條
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容