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

      





posted @ 2015-05-18 19:42  梅尔加德斯  阅读(308)  评论(0编辑  收藏  举报