MongoDB安裝使用教程


Windows版本:

1. 下載:

https://www.mongodb.com/download-center/community

 

2. 配置安裝:

  a. 解壓后創建分別創建文件夾用於存放數據庫文件和日志文件:

    創建存放數據庫文件夾: D:\installUtils\mongodb_4.2.0\data\db

    創建存放日志文件夾: D:\installUtils\mongodb_4.2.0\data\log,然后在log文件夾下新建一個名為 mongodb.log 的文件

    如圖:

           

  b. 將 MongoDB 安裝目錄下的 bin 目錄添加到環境變量 Path 中:

    

 

   c. win+R打開cmd輸入mongo -help查看是否配置成功:

    

  d. 配置啟動服務:

    以管理員的身份打開cmd,輸入以下代碼,注意根據自己的安裝路徑配置

mongod --logpath "D:\installUtils\mongodb_4.2.0\data\log\mongodb.log" --logappend --dbpath "D:\installUtils\mongodb_4.2.0\data\db" --directoryperdb --install

    

 

   e. 啟動服務:

net start MongoDB

    

 

   f. 停止服務:

net stop MongoDB

    

 

 


 

MongoDB常用命令:

db    
顯示當前的數據庫名稱

show dbs    
顯示當前服務器下數據庫(非空的數據庫)列表

use test    
如果test數據庫不存在,則創建test數據庫
如果test已存在,則切換到test數據庫

show collections    
顯示當前數據庫下所包含的集合(表)列表

db.users.insert({name:'zhangsha'})    
向users集合中插入數據
如果users集合存在,則直接插入數據,如果不存在,則創建users集合再插入數據

db.createCollection('products')
創建一個空集合products

db.products.insert([{name:'lishi'},{name:'wangwu'}])        
一次插入多個數據

db.products.find()
查詢products集合中所有的數據

db.products.find({name:'蘋果手機'})
查詢stu集合中name='蘋果手機'的數據

db.products.find({name:{$eq:'蘋果手機'}})
同上,$eq=>等號,建議使用上面的方式,易記,易輸入
eq = equal

db.products.find({price:{$gt:18}})
查詢stu集合中age>18的數據  
 
把$gt換成如下的符號試試:
$gt=>大於   great
$gte=>大於等於 great equal
$lt=>小於   less than
$lte=>小於等於 less than equal
$ne=>不等於  not equal
$in=>在范圍內
$nin=>不在范圍內
以上幾個符號格式總結為:{ field: {符號: value}}

db.products.find({name:/^華為/})
查找stu集合中name域中以“華為”字符的開頭的數據

db.products.find({name:{$in:['手機1','手機2']}})
查詢stu集合中name='手機1'和name='手機2'的數據
$in=>在范圍內
$nin=>不在范圍內
以上兩個符號格式為:{ field:{符號:[value1,value2,....]}}

db.products.find({name:"華為手機",price:800})
查找name="華為手機"並且price:800的數據

db.products.find({$or:[{name:'華為手機'},{price:{$lt:1000}}]})
查詢products集合中name='華為手機' 或者 price<1000的數據
$or=>或者  注意$or:[{},{},....]
$and=>並且  格式同$or, 例:{$and:[{},{},....]}
$nor=>not or 與$or相反, 格式同$or

db.products.find({price:{$not:{$gt:100}}})
查詢products集合中price<=100的數據,不存在price屬性的數據也會查詢出來
$not=>取反 

db.products.find({price:{$exists: true}})
查詢products集合中包含域名稱為price的數據

db.products.find({name:{$type:2}})
查詢products集合中name屬性為字符串類型的數據

db.products.find({
    $where: function(){
        return this.name == '華為手機'
    }
})
查詢products集合中name='華為手機’的數據

db.products.find({
    $where: function(){
         return  this.name.indexOf('華為手機') > -1;
    }
})
查詢products集合中name域中包含“華為手機”字符的數據

db.products.update({name:'華為手機'},{$set:{price:2000}},{
    upsert: true,
    multi:false
})
把products集合中name='華為手機'的那條數據,把price屬性設置成2000,其它屬性保留
$set是指更改的屬性列表,不在列表中其他屬性會被保留,如果不加此符號,其它屬性會被丟棄(_id屬性比較特殊,不會丟失)
upsert:true如果沒有符號條件的更新時,則插入一條,為false時,則不會插入, 默認是false
multi:false一次只能更新一條數據,為true時,可更新多條,默認是false

db.students.remove({})
清空集合students

db.products.remove({name:'abc'})
刪除products集合中name='abc'的數據,注意,即使把集合products中的所有數據都刪除了
products集合仍然存在, remove()是用來刪除數據的,而drop()不僅會刪除數據,還會把
集合的結構給刪除

db.products.drop()
把stu集合徹底從當前數據中刪除,集合stu不再存在,注意與remove()的區別

db.dropDatabase()
刪除當前數據庫

db.users.distinct('name')
查詢users集合中不重復的name屬性,返回的是數組

db.stu.count({name:'zhangshan'})
查詢stu集合中name='zhangshan'的數據數量

db.stu.find().limit(5)
查詢stu集合中前5條數據

db.stu.find().skip(5)
查詢stu集合中跳過前5條后的數據

db.stu.find().sort({name:1})
查詢stu集合中的全部數據,並按name屬性正序排列  注:1:正序 -1: 倒序

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM