MongDB004 Shell基本操作入门

来源:互联网 发布:ios的一个编程游戏 编辑:程序博客网 时间:2024/04/29 19:44

在Shell查看操作数据会用到4个基本操作:创建、读取、更新和删除(CRUD)

1.      创建

Insert函数添加一个文档到集合里面。例如,假设要存储一管博客文章。首先,创建一个局部变量post,内容是代表文档的JavaScript对象。里面面会有”title”、”content”和”data”几个键。

//切换到foobar库上,如果没有,直接创建

>use foobar

//创建post文档

>post = {"title":"My Blog Post","content":"Here's my blog post.","date":newDate()}

//插入post到集合blog中

> db.blog.insert(post)

//查询db库中blog集合的文档

> db.blog.find()

//查询出来的结果

{"_id" : ObjectId("51a869dbda9e669153726036"),"title" : "My Blog Post", "conte

nt" : "Here's my blog post.","date" : ISODate("2013-05-31T09:13:41.434Z") }

2.      读取

>db.blog.findOne()

{

       "_id" : ObjectId("51a869dbda9e669153726036"),

        "title" : "My BlogPost",

        "content" : "Here's myblog post.",

        "date" :ISODate("2013-05-31T09:13:41.434Z")

}

find和findOne可以接受查询文档形式的限定条件,这将通过查询限制匹配的文档。具体的介绍见后面的文章。

3.      更新

//给post变量增加一个key-value

>post.comments=[]

[ ]

//将更改后的值入库

>db.blog.update({title:"My Blog Post"},post)

> db.blog.find()

{"_id" : ObjectId("51a869dbda9e669153726036"),"title" : "My Blog Post", "conte

nt": "Here's my blog post.", "date" :ISODate("2013-05-31T09:13:41.434Z"), "com

ments": [ ] }

update至少接受两个参数,第一个参数为限定条件,第二个是新的文档。

4.      删除

Remove用来从数据库中永久性地删除文档

>db.blog.remove({title:"My Blog Post"})

> db.blog.find()

 

 Shell使用技巧

5.      帮助

> help

        db.help()                    help on db methods

        db.mycoll.help()             help on collection methods

        rs.help()                    help on replica set methods

        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 currentdatabase

        show users                   show users in currentdatabase

        show profile                 show most recentsystem.profile entries with time >= 1ms

        show logs                    show the accessible loggernames

        show log [name]              prints out the last segment oflog in memory, 'global' is default

        use <db_name>                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 lastline evaluated; use to further iterate

        DBQuery.shellBatchSize = x   set default number of items to display onshell

        exit                         quit the mongo shell

使用db.help()可以查看数据库级别的命令的帮助,集合的相关帮助可以通过db.foo.help来查看。有个了解功能的技巧,就是在输入的时候不要输入括号民,这样就会显示该函数的JavaScipt源代码。例如,如果想看看update的机理,或者就是为了看看参数顺序,可以这么做>db.foo.update

 

注意:使用db.集合名的方式来访问集合一般不会有问题,但如果集合名恰好是数据库类的一个属性就有问题了。例如,要访问version这个集合,使用db.version就不行,因为db.version是个数据库函数,这时候可以使用getCollection函数

> db.getCollection("version")

foobar.version

> show dbs

foobar  0.03125GB

local   (empty)

如果集合中带好特殊字符也需要使用上述方法来获取集合,如

>db.getCollection(“foo-bar”)