最近接触了一些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

mongo DB的一般操作的更多相关文章

  1. Mongo DB Java操作

    1.首先下载Mongo DB java 驱动 2.操作Mongo 增删改查 package com.sjjy.mongo; import java.util.ArrayList;import java ...

  2. 文件存储Mongo DB后前端对于文件操作的处理方式

    以下是关于后端对于附件从存储服务器改为存储到Mongo DB后,前端对于一些常见需求处理方式的修改:包括文件上传下载和富文本编辑中的贴图实现. 一.文件上传(记录关于fetch中post请求Conte ...

  3. Mongo DB 2.6 需要知道的一些自身限定

    在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...

  4. mongo db 使用方法

    1 下载 mogodb http://www.mongodb.org/display/DOCS/Downloads 2 打开服务 我安装在e盘下了 可以指定数据文件位置 到 E:\mongoDB\mo ...

  5. Java从入门到精通——数据库篇Mongo DB 导出,导入,备份

    一.概述    本篇博客为大家讲述一下Mongo DB是如何导入导出数据,还有就是备份数据的.    在下面操作的时候需要把Mongo DB的服务端打开才能操作. 二.导出.    MongoDB的导 ...

  6. mongo db 使用方法[转]

    1 下载 mogodb http://www.mongodb.org/display/DOCS/Downloads 2 打开服务 我安装在e盘下了 可以指定数据文件位置 到 E:\mongoDB\mo ...

  7. Mongo db 简单介绍及命令笔记

    首先来了解下什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为W ...

  8. C# Mongo DB 修改多层嵌套集合中的字段

    C# Mongo DB 修改嵌套集合中的字段 虽然c#的mongo 驱动很强大,而且还支持linq,但是一些复杂的操作语句还是比较困难 这里我用Bson实现功能 这是模型(我这里有多层嵌套) publ ...

  9. mongo DB for C#

    (1)Download the MongoDB C#驱动. http://www.nuget.org/packages/mongocsharpdriver/. (2) Add Reference to ...

随机推荐

  1. 第一章 Linux內核簡介

    1. Linux是類Unix系統,但他不是Unix. 儘管Linux借鑑了Unix的許多設計並且實現了Unix的API(由Posix標準和其他Single Unix Specification定義的) ...

  2. Oracle在线重定义DBMS_REDEFINITION 普通表—>分区表

    实验环境:RHEL 6.4 + Oracle 11.2.0.3实验:在线重定义 普通表 为 分区表,包括主键对应的索引都改造为分区索引. 1,构造普通表t_objects conn test1/tes ...

  3. Oracle数据库全球化

    一.自定义数据库和各个会话的语言相关行为 1.1 全球化功能 1.2 使用全球化支持功能 二.使用数据库和NLS字符集 2.1 语言排序与选择 2.2 Locale Builder 2.3 使用时区 ...

  4. 相克军_Oracle体系_随堂笔记014-锁 latch,lock

    1.Oracle锁类型 2.行级锁:DML语句 3.表级锁:TM 4.锁的兼容性 5.加锁语句以及锁的释放 6.锁相关视图 7.死锁 1.Oracle锁类型 锁的作用     latch锁:chain ...

  5. C#枚举类型的常用操作总结

    枚举类型是定义了一组"符号名称/值"配对.枚举类型是强类型的.每个枚举类型都是从system.Enum派生,又从system.ValueType派生,而system.ValueTy ...

  6. 设置 LongListSelector 只有在项多的时候才分组

    Windows Phone 中的控件LongListSelector是一个很好的分组聚类控件,当列表中数据特别多的时候,LongListSelector就像字典中的目录,让我们很快定位到要找的数据. ...

  7. .NET 扩展方法 (二)

    上一篇随笔 .NET 扩展方法 (一) 已经对 扩展方法有了大致的介绍,这篇算是一个补充,让我们来看一下扩展方法的几个细节: 一.扩展方法具有继承性 当使用扩展方法扩展一个类型的时候,其也扩展了派生类 ...

  8. 使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和解密

    在数据库实现加密与解密的文章,Insus.NET较早前也有写过,可以在本博客中可以搜索得到. 今天使用EncryptByPassPhrase和DecryptByPassPhrase来简单实现. 在数据 ...

  9. customErrors与错误页面

    本配置节相对简单而且常用 <customErrors defaultRedirect="url" mode="On|Off|RemoteOnly"> ...

  10. assign()