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


相关推荐

  • Python基础 知识点总结[通俗易懂]

    Python基础 知识点总结[通俗易懂]python入门、基础知识

    2022年10月9日
  • 数据库设计 ER图

    数据库设计 ER图一、ER图简介ER图,简单来说,E是实体,实体有一组属性;R是关系。找打系统中的实体以及实体关系就可以绘制出ER图了。例如,下图是网上找到的ER图,矩形的是实体,椭圆是属性,实体何实体时间的关系用菱形,关系也有熟悉,例如,学生选修课程,有成绩属性,当然如果系统需要,也可以记录选修的时间等属性信息。认真看下,你会发现ER图理解起来还是比较容易的二、ER图绘制常见问题但是真的落实到自己绘制,很多同学就会遇到困难。下面我们通过反例来学习ER图1.反例1区分功能和关系.

    2022年6月21日
  • double转bigDecimal精度问题

    double转bigDecimal精度问题double转bigDecimal精度问题需要用到bigDecimal的字符串构造来转float的精度:2^237位double的精度:2^5216位十进制转二进制存在精度差doubleg=12.35;BigDecimalbigG=newBigDecimal(g).setScale(1,BigDecimal.ROUND_HALF_UP);//…

    2022年6月4日
  • 分享我常用的5个免费的在线 SQL 数据库环境,简直太方便了!

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:不剪发的Tony老师 blog.csdn.net/horses/article/details/1086039…

    2021年6月25日
  • python 生成exe文件

    python 生成exe文件在windows下,可以使用pyinstaller打包python程序为exe可执行程序。1、安装pyinstaller在cmd命令行窗口运行以下命令安装pyinstallerpipinstallpyinstaller2、打包python程序在python程序所在目录,执行以下命令#切换到指定目录cd/dpath#正常打包命令pyinstaller-F-w-iico_pathxxx.py-F是将所有文件打成一个exe文件,一般是必写的(注意必须

    2022年4月29日
  • 请说下封装 vue 组件的过程?_组件二次封装的必要性

    请说下封装 vue 组件的过程?_组件二次封装的必要性vue组件的定义●组件(Component)是Vue.js最强大的功能之一●组件可以扩展HTML元素,封装可重用代码●在较高层面上,组件是自定义元素,Vue.js的编译器为他添加特殊功能●有些情况下,组件也可以表现用is 特性进行了扩展的原生的HTML元素●所有的Vue组件同时也都是Vue实例,所以可以接受相同的选项对象(除了一些根级特有的选项),并提供相同的生命周…

    2022年9月24日

发表回复

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

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