mongoDB常用密令

來源:互聯網
上載者:User

標籤:dbn   多少   總數   命令   尋找   獲得   原來   插入   move   

  1. 常用的命令
  2. show dbs    顯示資料庫列表

    use dbname    進入dbname資料庫,大小寫敏感,沒有這個資料庫也不要緊

    show collections    顯示資料庫中的集合,相當於表格

  3.  

    建立&新增

           db.users.save({"name":"lecaf"})    建立了名為users的集合,並新增了一條{"name":"lecaf"}的資料

           db.users.insert({"name":"ghost", "age":10})    在users集合中插入一條新資料,,如果沒有users這個集合,mongodb會自動建立

           save()和insert()也存在著些許區別:若新增的資料主鍵已經存在,insert()會不做操作並提示錯誤,而save() 則更改原來的內容為新內容。

    存在資料:{ _id : 1, " name " : " n1 "} ,_id是主鍵

    insert({ _id : 1, " name " : " n2 " })    會提示錯誤

    save({ _id : 1, " name " : " n2 " })     會把 n1 改為  n2 ,有update的作用。

  4.  

    刪除

    db.users.remove()    刪除users集合下所有資料

    db.users.remove({"name": "lecaf"})    刪除users集合下name=lecaf的資料

    db.users.drop()或db.runCommand({"drop","users"})    刪除集合users

    db.runCommand({"dropDatabase": 1})    刪除當前資料庫

  5.  

    尋找

    db.users.find()    尋找users集合中所有資料

    db.users.findOne()    尋找users集合中的第一條資料

  6.  

    修改

    db.users.update({"name":"lecaf"}, {"age":10})    修改name=lecaf的資料為age=10,第一個參數是尋找條件,第二個參數是修改內容,除了主鍵,其他內容會被第二個參數的內容替換,主鍵不能修改

  7.  

    條件尋找

          db.collection.find({ "key" : value })    尋找key=value的資料

          db.collection.find({ "key" : { $gt: value } })    key > value

          db.collection.find({ "key" : { $lt: value } })    key < value

          db.collection.find({ "key" : { $gte: value } })    key >= value

          db.collection.find({ "key" : { $lte: value } })    key <= value

          db.collection.find({ "key" : { $gt: value1 , $lt: value2 } })    value1 < key <value2

          db.collection.find({ "key" : { $ne: value } })    key <> value

          db.collection.find({ "key" : { $mod : [ 10 , 1 ] } })    模數運算,條件相當於key % 10 == 1 即key除以10餘數為1的

          db.collection.find({ "key" : { $nin: [ 1, 2, 3 ] } })    不屬於,條件相當於key的值不屬於[ 1, 2, 3 ]中任何一個

           db.collection.find({ "key" : { $in: [ 1, 2, 3 ] } })    屬於,條件相當於key等於[ 1, 2, 3 ]中任何一個

          db.collection.find({ "key" : { $size: 1 } })    $size 數量、尺寸,條件相當於key的值的數量是1(key必須是數組,一個值的情況不能算是數量為1的數組)

          db.collection.find({ "key" : { $exists : true|false } })    $exists 欄位存在,true返回存在欄位key的資料,false返回不存在字度key的資料

          db.collection.find({ "key": /^val.*val$/i })    正則,類似like;“i”忽略大小寫,“m”支援多行

          db.collection.find({ $or : [{a : 1}, {b : 2} ] })    $or或 (注意:MongoDB 1.5.3後版本可用),符合條件a=1的或者符合條件b=2的資料都會查詢出來

          db.collection.find({ "key": value , $or : [{ a : 1 } , { b : 2 }] })    符合條件key=value ,同時符合其他兩個條件中任意一個的資料

          db.collection.find({ "key.subkey" :value })    內嵌對象中的值匹配,注意:"key.subkey"必須加引號

          db.collection.find({ "key": { $not : /^val.*val$/i } })    這是一個與其他查詢條件組合使用的操作符,不會單獨使用。上述查詢條件得到的結果集加上$not之後就能獲得相反的集合。

  8.  

    排序

          db.collection.find().sort({ "key1" : -1 ,"key2" : 1 })    這裡的1代表升序,-1代表降序8

  9. 其他

          db.collection.find().limit(5)    控制返回結果數量,如果參數是0,則當作沒有約束,limit()將不起作用

          db.collection.find().skip(5)    控制返回結果跳過多少數量,如果參數是0,則當作沒有約束,skip()將不起作用,或者說跳過了0條

          db.collection.find().skip(5).limit(5)    可用來做分頁,跳過5條資料再取5條資料

          db.collection.find().count(true)    count()返回結果集的條數

          db.collection.find().skip(5).limit(5).count(true)    在加入skip()和limit()這兩個操作時,要獲得實際返回的結果數,需要一個參數true,否則返回的是符合查詢條件的結果總數

     

mongoDB常用密令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.