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


相关推荐

  • gcc的编译命令_cmake 编译

    gcc的编译命令_cmake 编译GCC编译命令                    —————-加入新公司后,基本上是一键式打包脚本,对于GCC基本上快忘了,重新拾起。GCC命令提供了非常多的命令选项,但并不是所有都要熟悉,初学时掌握几个常用的就可以了,到后面再慢慢学习其它选项,免得因选项太多而打击了学习的信心。一.常用编译命令选项假设源程序文件名为test.c。1…

    2022年10月13日
    0
  • SpringCloud(三)—-负载均衡解决方案分类及特征

    SpringCloud(三)—-负载均衡解决方案分类及特征

    2020年11月12日
    175
  • Matlab画图颜色「建议收藏」

    Matlab画图颜色「建议收藏」matlab绘图配色

    2022年5月6日
    284
  • C++函数模板(模板函数)详解

    C++函数模板(模板函数)详解定义用法:函数模板的原理延申用法2.1为什么需要类模板2.2单个类模板语法2.3继承中的类模板语法案例1:案例2:2.4类模板的基础语法2.5类模板语法知识体系梳理1.所有的类模板函数写在类的内部复数类:2.所有的类模板函数写在类的外部,在一个cpp中2.5总结关于类模板的几点说明:2.6类模板中的static关键字案例2:以下来自:C++类模板遇上static关键字…

    2022年4月4日
    45
  • 2188. 无源汇上下界可行流(无源汇上下界最大流)

    2188. 无源汇上下界可行流(无源汇上下界最大流)给定一个包含 n 个点 m 条边的有向图,每条边都有一个流量下界和流量上界。求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限制。输入格式第一行包含两个整数 n 和 m。接下来 m 行,每行包含四个整数 a,b,c,d 表示点 a 和 b 之间存在一条有向边,该边的流量下界为 c,流量上界为 d。点编号从 1 到 n。输出格式如果存在可行方案,则第一行输出 YES,接下来 m 行,每行输出一个整数,其中第 i 行的整数表示输入的第 i 条边的流量。如果不存在可行方案,直接输

    2022年8月9日
    5
  • 微信上赚钱需要准备什么?[通俗易懂]

    微信上赚钱需要准备什么?[通俗易懂]微信上赚钱需要准备什么?1、一个风口上的产品2、微信好友人脉资源3、群发工具其实很多时候赚钱并没有那么难,想在微信上做生意、做社交其实很简单。很多人目前可能手头都有至少一个产品是可以卖的,但是他们没有有效的利用起来自己的微信人脉。也许是不会;也许是会,但是没工具不能;也许就是懒……不管出于什么原因,我这篇文章是写给想赚钱的人。加余老师VX:125381839微精灵营销工具可以帮助我们做哪些…

    2022年6月4日
    37

发表回复

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

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