mongoDB數據庫文件路徑和數據操作


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 } )

 


免責聲明!

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



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