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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux制作img镜像文件_linux用命令打开浏览器

    linux制作img镜像文件_linux用命令打开浏览器linuxrootfs.img的制作cramfs是只读压缩的文件系统,文件系统类型可以是ext2,ext3,什么的, cramfs和romfs只是一个文件系统类型,ramdisk相当于一块硬盘空间,可以理解为在内存中虚拟出一块硬盘来,所以它上面就可以有你linux支持的各种文件系统什么的。所以你问的,它和romfs和cramfs确实不是一个层次的概念。^-^恭喜你,你答

    2022年9月27日
    3
  • 图书馆管理系统需求规格说明书

    图书馆管理系统需求规格说明书青院图书馆信息管理系统软件需求规格说明书文档编号 QY LY7 文档信息 图书馆信息管理系统软件需求规格说明书文档类别 管理文档密 nbsp nbsp nbsp nbsp 级 机密版本信息 1 0 建立日期 2014 05 20 nbsp 创 nbsp 建 nbsp 人 审 nbsp 核 nbsp 者 批 nbsp 准 nbsp 人 批准日期 nbsp 编辑软件 Microsoft nbsp Office nbsp 2003 nbsp 中文版 WPS nbsp 文字

    2025年9月2日
    3
  • java8 Lambda表达式

    java8 Lambda表达式java8 Lambda表达式

    2022年4月23日
    45
  • 软件概要设计与详细设计

    (一)概要设计的任务与步骤1、总体设计的必要性:可以站在全局角度上,花较少成本,从抽象的层次上分析对比多种可能性的系统实现方案和软件结构,从中选出最佳方案和最合理的软件结构,从而用较低成本开发出较高质量的软件系统。2、总体设计的两个阶段:(1)系统设计阶段:确定系统的具体实现方案(2)结构设计阶段:确定软件结构。3、总体设计的9个步骤:(1)设想供选择的方案(2)选取…

    2022年4月6日
    46
  • java 异常分类和处理机制

    java 异常分类和处理机制一、背景介绍程序在运行过程中发生错误或异常情况是不可避免的,如果每一个运行时错误都由程序员手动控制和处理,其工作量是不可想象的。Java语言中的异常处理机制就解决的上述问题,把错误与异常的管理带到了面向对象的世界Java语言定义了很多异常类,将运行错误和异常的信息和处理方法封装在了异常类中,帮助程序员检查和控制异常。即J…

    2022年5月13日
    47
  • 剑指offer—12-**–数值的整数次方

    剑指offer—12-**–数值的整数次方

    2021年6月9日
    88

发表回复

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

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