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)
上一篇 2021年11月26日 下午2:00
下一篇 2021年11月26日 下午3:00


相关推荐

  • 《前端运维》一、Linux基础–11服务

    首先服务的概念,对于linux服务器来说,其中大多数的软件都算是服务,比如nginx、mysql、Jenkins等等。下面我们就来学习一下linux中有关服务的一些内容。一、服务简介和分类1、运行

    2022年3月25日
    38
  • 如何入门网络安全_网络安全自学

    如何入门网络安全_网络安全自学由于我之前写了不少网络安全技术相关的故事文章,不少读者朋友知道我是从事网络安全相关的工作,于是经常有人在微信里问我:我刚入门网络安全,该怎么学?要学哪些东西?有哪些方向?怎么选?不同于Java、C/C++等后端开发岗位有非常明晰的学习路线,网路安全更多是靠自己摸索,要学的东西又杂又多,难成体系。常读我文章的朋友知道,我的文章基本以故事为载体的技术输出为主,很少去谈到职场、面试这些方面的内容。主要是考虑到现在大家的压力已经很大,节奏很快,公众号上是让大家放松的地方,尽量写一些轻快的内容。不

    2022年10月21日
    6
  • typedef enum的用法(枚举)

    typedef enum的用法(枚举)include stdio h include string h include stdlib h intmain typedefenum saturday sunday 0 monday tuesday wednesday thursday friday workday stdlib h string h stdio h

    2025年12月9日
    8
  • 海外网友试用Manus:印象深刻,但仍处于早期阶段

    海外网友试用Manus:印象深刻,但仍处于早期阶段

    2026年3月15日
    3
  • 什么是 Hook 技术

    什么是 Hook 技术一、什么是Hook技术  Hook技术又叫做钩子函数,在系统没有调用该函数之前,钩子程序就先捕获该消息,钩子函数先得到控制权,这时钩子函数既可以加工处理(改变)该函数的执行行为,还可以强制结束消息的传递。简单来说,就是把系统的程序拉出来变成我们自己执行代码片段。  要实现钩子函数,有两个步骤:  1.利用系统内部提供的接口,通过实现该接口,然后注入进系统(特定场景下使用)  2.动态代理(使用所有场景)二、Hook技术实现的步骤  Hook技术实现的步骤也分为两步  1.找到ho

    2022年5月13日
    43
  • ios消息推送机制_iPhone消息推送

    ios消息推送机制_iPhone消息推送原文地址:http://www.apkbus.com/android-130195-1-5.html推送是解决轮询所造成的流量消耗和电量消耗的一个比较好的解决方案,在Android上,虽然Google提供了GCM(之前为C2DM),但在国内基本等于没用,各大Android应用基本都自己架设推送Server或是使用第三方推送平台,例如新浪微博使用第三方推送平台“个推”(非广告 )。今天要

    2026年4月17日
    5

发表回复

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

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