1、查看MongoDB在電腦上的安裝路徑
which mongod
2、默認mongodb 數據文件是放到根目錄 data/db 文件夾下,如果沒有這個文件,需要自行創建
mkdir -p /data/db
3、或者,也可以在每次啟動時指定數據庫路徑:
mongod --dbpath /usr/local/db
4、插入數據:
db.student.insert({"name":"xiaoming"});
5、查詢數據的方法:
查找數據,用find。find中沒有參數,那么將列出這個集合的所有文檔: db.restaurants.find() 精確匹配: db.student.find({"score.shuxue":70}); 多個條件: db.student.find({"score.shuxue":70 , "age":12}) 大於條件: db.student.find({"score.yuwen":{$gt:50}}); 或者。尋找所有年齡是9歲,或者11歲的學生 db.student.find({$or:[{"age":9},{"age":11}]}); 查找完畢之后,打點調用sort,表示升降排序。 db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
6、修改數據:
修改里面還有查詢條件。要該誰,要告訴mongo。 db.student.update({"name":"小明"},{$set:{"age":16}}); 查找數學成績是70,把年齡更改為33歲: db.student.update({"score.shuxue":70},{$set:{"age":33}}); 更改所有匹配項目:" By default, the update() method updates a single document. To update multiple documents, use the multi option in the update() method. db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true}); 完整替換,不出現$set關鍵字了: db.student.update({"name":"小明"},{"name":"大明","age":16});
7、刪除數據
db.restaurants.remove( { "borough": "Manhattan" } ) By default, the remove() method removes all documents that match the remove condition. Use the justOne option to limit the remove operation to only one of the matching documents. db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )