注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

老狗的博客

尽管每一步都很微小,但我确认我在进步

 
 
 

日志

 
 
关于我
sky

认真生活,努力工作 热爱技术,关注DB,存储,分布式,中间层,java,c++,php

网易考拉推荐

mongodb学习  

2016-05-12 13:09:31|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

可以看到mongodb的命令中的命令采用驼峰命名法如getName, getReplicationInfo
1. db.stats
cmongo-test1_0:PRIMARY> db.stats()
{
        "db" : "test",
        "collections" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}
2. db.getName()
cmongo-test1_0:PRIMARY> db.getName()
test
3. db.createCollection(name, { size : ..., capped : ..., max : ... } )
cmongo-test1_0:PRIMARY> db.createCollection("student")
{ "ok" : 1 }
4. db.isMaster()
cmongo-test1_0:PRIMARY> db.isMaster()
{
        "hosts" : [
                "127.0.0.1:7000",
                "127.0.0.1:7001"
        ],
        "arbiters" : [
                "127.0.0.1:7002"
        ],
        "setName" : "cmongo-test1_0",
        "setVersion" : 4,
        "ismaster" : true,
        "secondary" : false,
        "primary" : "127.0.0.1:7001",
        "me" : "127.0.0.1:7001",
        "electionId" : ObjectId("7fffffff0000000000000002"),
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2016-05-12T01:57:20.738Z"),
        "maxWireVersion" : 4,
        "minWireVersion" : 0,
        "ok" : 1
}
5. db.hostInfo()
cmongo-test1_0:PRIMARY> db.hostInfo()
{
        "system" : {
                "currentTime" : ISODate("2016-05-12T01:58:33.601Z"),
                "hostname" : "",
                "cpuAddrSize" : 64,
                "memSizeMB" : 29904,
                "numCores" : 8,
                "cpuArch" : "x86_64",
                "numaEnabled" : false
        },
        "os" : {
                "type" : "Linux",
                "name" : "",
                "version" : "Kernel 3.10.83-1-tlinux2_kvm_guest-0015.tl2"
        },
        "extra" : {
                "versionString" : "",
                "libcVersion" : "2.17",
                "kernelVersion" : "",
                "cpuFrequencyMHz" : "2600.000",
                "cpuFeatures" : "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm xsaveopt fsgsbase smep erms",
                "pageSize" : NumberLong(4096),
                "numPages" : 7655539,
                "maxOpenFiles" : 1024
        },
        "ok" : 1
}
6. db.student.find()
列出表中所有的document

7. db.getReplicationInfo()
cmongo-test1_0:PRIMARY> db.getReplicationInfo()
{
        "logSizeMB" : 10,
        "usedMB" : 0.01,
        "timeDiff" : 674472,
        "timeDiffHours" : 187.35,
        "tFirst" : "Wed May 04 2016 14:58:20 GMT+0800 (CST)",
        "tLast" : "Thu May 12 2016 10:19:32 GMT+0800 (CST)",
        "now" : "Thu May 12 2016 10:21:21 GMT+0800 (CST)"
}

8. collection中插入数据
cmongo-test1_0:PRIMARY> db.student.insert({name:"liudong"})
WriteResult({ "nInserted" : 1 })

9. collection中查找数据
db.<collection_name>.find(query, projection)
a. query 可选,如果不指定,就是选出所有的document
b. projection可选,如果不指定,就是选出document中的所有字段,
cmongo-test1_0:PRIMARY> db.student.find({name:"liudong"})
{ "_id" : ObjectId("575e2ab7d5d0ae693342d12a"), "name" : "liudong" }
{ "_id" : ObjectId("575e3a34d5d0ae693342d12b"), "name" : "liudong" }

projection的格式为{ field1: 0/1, field2: 0/1}, 0代表排除,1代表包含,一个projection中不能同时包含排除和包含
cmongo-test1_0:PRIMARY> db.student.find({name:"liudong"}, {name:0})
{ "_id" : ObjectId("575e2ab7d5d0ae693342d12a") }
{ "_id" : ObjectId("575e3a34d5d0ae693342d12b") }
cmongo-test1_0:PRIMARY> db.student.find({name:"liudong"}, {name:0, _id:1})
Error: error: {
        "waitedMS" : NumberLong(0),
        "ok" : 0,
        "errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
        "code" : 2
}
10. 查看collection的状态
cmongo-test1_0:PRIMARY> db.student.stats()
{
        "ns" : "test.student",
        "count" : 3,
        "size" : 121,
        "avgObjSize" : 40,
        "storageSize" : 32768,
        "capped" : false,
        "wiredTiger" : {
            ...
        },
        "nindexes" : 1,
        "totalIndexSize" : 32768,
        "indexSizes" : {
                "_id_" : 32768
        },
        "ok" : 1
}
11. 连接mongod
mongo [option] [db address]
没有认证
mongo 127.0.0.1:7000
需要认证
mongo -u xxx -p xxx --authenticationDatabase xxx 127.0.0.1:7000
  评论这张
 
阅读(132)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018