设为首页 收藏本站
查看: 746|回复: 0

[经验分享] MongoDB学习笔记【2】-- 试用

[复制链接]
YunVN网友  发表于 2015-7-9 10:39:12 |阅读模式
  大部分内容根据MongoDB官方手册整理:http://docs.mongodb.org/manual/contents/
  查看数据库



[iyunv@slayer ~]# mongo
MongoDB shell version: 2.2.3
connecting to: test
> show dbs
local   (empty)
test    0.0625GB
>
  使用use切换数据库,use一个新名字可以使用新数据库,MongoDB会等到插入数据才会建立



> use mydb
switched to db mydb
> show dbs
local   (empty)
test    0.0625GB
> db.mydb.insert({name:"slayer"})
> show dbs
local   (empty)
mydb    0.0625GB
test    0.0625GB
>
  
  find看内容



> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
>
  换个方式插入。。



> a = {1 : "x"}
{ "1" : "x" }
> b = {2 : "y"}
{ "2" : "y" }
> db.mydb.insert(a, b)
> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
{ "_id" : ObjectId("50c951ba96c0253cce4a8c88"), "1" : "x" }
> db.mydb.insert(b)
> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
{ "_id" : ObjectId("50c951ba96c0253cce4a8c88"), "1" : "x" }
{ "_id" : ObjectId("50c951d096c0253cce4a8c89"), "2" : "y" }
  可以看到insert不支持多个参数
  用循环插入



> for (i=0; i db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
{ "_id" : ObjectId("50c951ba96c0253cce4a8c88"), "1" : "x" }
{ "_id" : ObjectId("50c951d096c0253cce4a8c89"), "2" : "y" }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8a"), "value" : 0 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8b"), "value" : 1 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8c"), "value" : 2 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8d"), "value" : 3 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8e"), "value" : 4 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8f"), "value" : 5 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c90"), "value" : 6 }
>
  继续插入



> db.mydb.xx.insert({1:3})
> db.mydb.xx.find()
{ "_id" : ObjectId("50c966b596c0253cce4a8c91"), "1" : 3 }
  查看当前数据库 db 删除数据库 db.dropDatabase()



> db
mydb
> show dbs
local   (empty)
mydb    0.0625GB
test    0.0625GB
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
> show dbs
local   (empty)
test    0.0625GB
> db
  
  使用游标和循环来打印内容



> var c = db.mydb.find()
> while (c.hasNext()) printjson(c.next())
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c93"), "value" : 1 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c94"), "value" : 2 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c95"), "value" : 3 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c96"), "value" : 4 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c97"), "value" : 5 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c98"), "value" : 6 }
>
  三个方法的官方说明:The hasNext() function returns true if the cursor has documents. The next() method returns the next document. The printjson() method renders the document in a JSON-like format.
  hasNext() 返回游标是否指向文件,next 返回下一个文件。printjson 把文件用用类json格式打印出来。
  使用游标的下标



> var c = db.mydb.find()
> printjson(c[0])
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
> printjson(c[4])
{ "_id" : ObjectId("50c96b7196c0253cce4a8c96"), "value" : 4 }
  
  打印复杂点的json数据



> db.mydb.xx.insert({a : 1, b: {A : "x", B : {alpha : 0}}})
> var c = db.mydb.xx.find()
> printjson(c.next())
{
"_id" : ObjectId("50c96eee96c0253cce4a8c99"),
"a" : 1,
"b" : {
"A" : "x",
"B" : {
"alpha" : 0
}
}
}
>
  
  查找元素



> db.mydb.find({value:0})
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
> db.mydb.insert({value:0, xx:"a"})
> db.mydb.find({value:0})
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
{ "_id" : ObjectId("50c9707396c0253cce4a8c9a"), "value" : 0, "xx" : "a" }
> db.mydb.find({value:0}).limit(1)
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
>
  
  
  最后看看各种help..



> help
db.help()                    help on db methods
db.mycoll.help()             help on collection methods
sh.help()                    sharding helpers
rs.help()                    replica set helpers
help admin                   administrative help
help connect                 connecting to a db help
help keys                    key shortcuts
help misc                    misc things to know
help mr                      mapreduce
show dbs                     show database names
show collections             show collections in current database
show users                   show users in current database
show profile                 show most recent system.profile entries with time >= 1ms
show logs                    show the accessible logger names
show log [name]              prints out the last segment of log in memory, 'global' is default
use                 set current database
db.foo.find()                list objects in collection foo
db.foo.find( { a : 1 } )     list objects in foo where a == 1
it                           result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x   set default number of items to display on shell
exit                         quit the mongo shell
>
  
  
  MongoDB学习笔记【1】-- 安装启动
  MongoDB学习笔记【2】-- 试用
  MongoDB学习笔记【3】-- MongoDB C驱动使用
MongoDB学习笔记【4】-- MongoDB Java驱动使用
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-84715-1-1.html 上篇帖子: 第一篇:MongoDB的安装 下篇帖子: MongoDB下载与安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表