MongoDB基礎--更新和刪除文檔

1,刪除數據庫: db.dropDatabase()數據庫

  1. > use Chenshi
    switched to db Chenshi
    > db
    Chenshi
    > show dbs
    local0.078125GB
    > db.computer.insert({"name":"lisi"})
    > show dbs
    Chenshi0.203125GB
    local0.078125GB
    > db.dropDatabase()
    {"dropped":"Chenshi","ok":1}
    > show dbs
    local0.078125GB

     

  2. 2,建立集合: db.createCollection(name, options)
  •     name:建立的集合名稱。
  •     options:初始化的文檔(可選)
  1. > db.createCollection("shiyanlou")
    {"ok":1}
    > show collections
    shiyanlou
    system.indexes
    > db.createCollection("shiyanlou2",{capped:1, autoIndexID:1, size:6142800, m
    ax :10000})
    {"ok":1}

     

描述:
  1. capped:類型爲Boolean,若是爲ture則建立一個固定大小的集合,當其條目達到最大時能夠自動覆蓋之前的條目。在設置其爲ture時也要指定參數大小;
    autoIndexID:類型爲Boolean,默認爲false,若是設置爲ture,則會在_id field.s上自動建立索引;
    size:若是capped爲ture須要指定,指定參數的最大值,單位爲byte;
    max:指定最大的文檔數。在mogodb中也能夠不用建立集合,由於在建立文檔的時候也會自動的建立集合

     

3,刪除集合: db.colleciontName.drop()
  1. > use shiyanlou
    switched to db shiyanlou
    > show collections
    shiyanlou2
    system.indexes
    > db.shiyanlou2.drop()
    true
    > db.shiyanlou.drop()
    false
    

      

 
4,插入文檔:db.collectionName.insert(document)
  1. > userdoc1=({"user_id":1,"name":"cloud","state":"active","actor":"user"
    ,"email":"test@qq.com","VM_num":2,"time":[{"date":"2014-05-56","hour":
    "5:34 PM"}]})
    {
    "user_id":1,
    "name":"cloud",
    "state":"active",
    "actor":"user",
    "email":"test@qq.com",
    "VM_num":2,
    "time":[
    {
    "date":"2014-05-56",
    "hour":"5:34 PM"
    }
    ]
    }
    > use Chenshi
    switched to db Chenshi
    > db.shiyanlou.insert(userdoc1)
    > db.shiyanlou.insert(userdoc2)
    MonMay1819:36:25.849ReferenceError: userdoc2 is not defined
    > db.shiyanlou.insert(userdoc1)
    

      

5,替換已經存在的文檔: db.collection_name.save({_id: ObjectId(), new_data})
6,刪除文檔: db.collection_name.remove( )
  1. > db.shiyanlou.remove({"name":"Bob"})
    

      



相關文章
相關標籤/搜索