mongo DB的一般操作

mongo DB的一般操作

最近接触了一些mongoDB 。将一些指令操作记录下来,便于查询和使用

 

登录

[root@logs ~]# mongo -u loguser -p log123456 –authenticationDatabase admin
MongoDB shell version: 2.4.10
connecting to: test
> show users
> post = {“title”:”My Blog Post”,”Content”:”Here is my blog Post.”,”Date”:new Date()}
{
        “title” : “My Blog Post”,
        “Content” : “Here is my blog Post.”,
        “Date” : ISODate(“2015-02-11T03:12:03.061Z”)
}

插入
–插入文档对象
> db.blog.insert(post)
> post = {“title”:”Licz Blog Post”,”Content”:”Here is my blog Post.”,”Date”:new Date()}
{
        “title” : “Licz Blog Post”,
        “Content” : “Here is my blog Post.”,
        “Date” : ISODate(“2015-02-11T03:17:07.219Z”)
}
> db.blog.insert(post)

读取
–读取集合里一个文档
> db.blog.findOne()
{
        “_id” : ObjectId(“54dac88dc956bbcbefa8151c”),
        “title” : “My Blog Post”,
        “Content” : “Here is my blog Post.”,
        “Date” : ISODate(“2015-02-11T03:12:03.061Z”)
}

–读取限定文档数
> db.blog.find().limit(100);
> db.blog.find().limit(100);
{ “_id” : ObjectId(“54dac88dc956bbcbefa8151c”), “title” : “My Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:12:03.061Z”) }
{ “_id” : ObjectId(“54dac9b8c956bbcbefa8151d”), “title” : “Licz Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:17:07.219Z”) }

–读取所有文档数
> db.blog.find()
{ “_id” : ObjectId(“54dac88dc956bbcbefa8151c”), “title” : “My Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:12:03.061Z”) }
> db.blog.find().limit(100);
{ “_id” : ObjectId(“54dac88dc956bbcbefa8151c”), “title” : “My Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:12:03.061Z”) }
{ “_id” : ObjectId(“54dac9b8c956bbcbefa8151d”), “title” : “Licz Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:17:07.219Z”) }

更新

–修改变量post,增加comments键
> post
{ “title” : “You Blog Post”, “Date” : ISODate(“2015-02-11T03:18:10.509Z”) }
> post.comments=[]
[ ]
> db.blog.update({title:”You Blog Post”},post)
> db.blog.find()
{ “_id” : ObjectId(“54dac88dc956bbcbefa8151c”), “title” : “My Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:12:03.061Z”) }
{ “_id” : ObjectId(“54dac9b8c956bbcbefa8151d”), “title” : “Licz Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:17:07.219Z”) }
{ “_id” : ObjectId(“54dac9f8c956bbcbefa8151e”), “title” : “You Blog Post”, “Date” : ISODate(“2015-02-11T03:18:10.509Z”), “comments” : [ ] }

删除

–删除title限定条件的文档
> db.blog.remove({title:”You Blog Post”})
> db.blog.find()
{ “_id” : ObjectId(“54dac88dc956bbcbefa8151c”), “title” : “My Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:12:03.061Z”) }
{ “_id” : ObjectId(“54dac9b8c956bbcbefa8151d”), “title” : “Licz Blog Post”, “Content” : “Here is my blog Post.”, “Date” : ISODate(“2015-02-11T03:17:07.219Z”) }

MongoDB使用技巧

–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 <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 last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

–特殊集合名处理
如果集合名恰好是和数据库类的一个属性名相同,可以使用db.getCollection进行访问

> db.version
function (){
    return this.serverBuildInfo().version;
}
> db.getCollection(“version”)
test.version

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119710.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CSDN更换博客皮肤教程

    CSDN更换博客皮肤教程1.进入博客设置,按F12键打开开发者工具调试页面2.找到博客皮肤的div,鼠标右键编辑HTML3.将checked=“checked”readonly=””剪切到你想要的皮肤里面,这里我是技术黑板的皮肤,之前就改了4.点击左边的页面,可以看到想要的皮肤是选中状态,然后点击保存,皮肤更改成功!…

    2022年7月14日
    19
  • PL SQLDEVELOPMENT导出数据库脚本

    PL SQLDEVELOPMENT导出数据库脚本

    2022年1月15日
    39
  • rehash过程_contenthash

    rehash过程_contenthash步骤1)首先创建一个比现有哈希表更大的新哈希表(expand)2)然后将旧哈希表的所有元素都迁移到新哈希表去(rehash)dictAdd对字典添加元素的时候,_dictExpandIfNeeded会

    2022年8月3日
    5
  • 简述MVC三层架构[通俗易懂]

    简述MVC三层架构[通俗易懂]MVC三层架构什么是MVC:ModelviewController模型、视图、控制器1、早些年用户直接访问控制层,控制层就可以直接操作数据库;servlet–CRUD–>数据库弊端:程序十分臃肿,不利于维护servlet的代码中:处理请求、响应、视图跳转、处理JDBC、处理业务代码、处理逻辑代码架构:没有什么是加一层解决不了的!程序猿调用|JDBC|MysqlOracleSqlServer….2、MVC三层架构Model

    2022年6月25日
    31
  • 随机函数生成随机正负小数(c语言自动生成随机数)

    原文转自:http://zhidao.baidu.com/question/560968122001783204.htmlC语言生成的随机数为无符号数,即都是正的。要想生成正负随机的,可以先通过模除限定返回,再减去对应的范围的中间值即可。比如,要获取-1000~+1000范围的随机数,总的数量为2001个,这样就可以通过代码rand()%2001使得到的结果限制在0-2000范围

    2022年4月16日
    86
  • c++ offsetof_函数offset的用法

    c++ offsetof_函数offset的用法宏offsetof标准库stddef.h定义size_toffsetof(type,member);分析C库宏offsetof(type,member)会生成一个类型为size_t的整型常量,它是一个结构成员(member)相对于结构(type)开头的字节偏移量。成员是由member给定的,结构的名称是在type中给定的。type–这是一个参数class类型,是结…

    2022年8月22日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号