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
mongo DB的一般操作的更多相关文章
- Mongo DB Java操作
1.首先下载Mongo DB java 驱动 2.操作Mongo 增删改查 package com.sjjy.mongo; import java.util.ArrayList;import java ...
- 文件存储Mongo DB后前端对于文件操作的处理方式
以下是关于后端对于附件从存储服务器改为存储到Mongo DB后,前端对于一些常见需求处理方式的修改:包括文件上传下载和富文本编辑中的贴图实现. 一.文件上传(记录关于fetch中post请求Conte ...
- Mongo DB 2.6 需要知道的一些自身限定
在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...
- mongo db 使用方法
1 下载 mogodb http://www.mongodb.org/display/DOCS/Downloads 2 打开服务 我安装在e盘下了 可以指定数据文件位置 到 E:\mongoDB\mo ...
- Java从入门到精通——数据库篇Mongo DB 导出,导入,备份
一.概述 本篇博客为大家讲述一下Mongo DB是如何导入导出数据,还有就是备份数据的. 在下面操作的时候需要把Mongo DB的服务端打开才能操作. 二.导出. MongoDB的导 ...
- mongo db 使用方法[转]
1 下载 mogodb http://www.mongodb.org/display/DOCS/Downloads 2 打开服务 我安装在e盘下了 可以指定数据文件位置 到 E:\mongoDB\mo ...
- Mongo db 简单介绍及命令笔记
首先来了解下什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为W ...
- C# Mongo DB 修改多层嵌套集合中的字段
C# Mongo DB 修改嵌套集合中的字段 虽然c#的mongo 驱动很强大,而且还支持linq,但是一些复杂的操作语句还是比较困难 这里我用Bson实现功能 这是模型(我这里有多层嵌套) publ ...
- mongo DB for C#
(1)Download the MongoDB C#驱动. http://www.nuget.org/packages/mongocsharpdriver/. (2) Add Reference to ...
随机推荐
- Anliven - 一碗毒鸡汤
什么是你的核心动力,支撑着你持续前进? 什么是你的加速度,激发你全部的潜能和勇气? 你的核心动力应该来自于: 家人与朋友的信任.包容与期待 你本应承担的责任 对自己有所要求,有所期待,你本应更好 而你 ...
- CSS表格溢出省略号代替及其他标签
box-shadow: 20px 20px 55px #777; // div阴影效果 filter:alpha(opacity=60) //设置透明度为60 opacity:0.6 //非IE浏览器 ...
- Android从网络某个地址下载文件、写入SD卡
首先创建一个HttpDownloader类,获取下载文件的网络地址,将文件下载下来以String流的方式返回: public String download(String urlStr){ //url ...
- Fragment基础----创建
1,Fragment的目的及应用场景 fragment 是3.0后引入的类,其字面翻译为“碎片”. 目的是将activity划分成许多单元再进行组合,可以根据不同分辨率屏幕,在不同状态下,灵活创建优化 ...
- ime-mode:disabled 关闭文本框输入法
在用户输入数字的表单中,需要禁止用户输入中文.符号等,减少用户输入出错误的可能性,CSS可以实现此功能. ime-mode的语法解释如下: ime-mode : auto | active | ina ...
- 一言不合就动手系列篇一-仿电商平台前端搜索插件(filterMore)
话说某年某月某日,后台系统需要重构,当时公司还没有专业前端,由我负责前台页面框架搭建,做过后台系统的都知道,传统的管理系统大部分都是列表界面和编辑界面.列表界面又由表格和搜索框组成, 对于全部都是输入 ...
- Design Patterns Simplified - Part 2 (Singleton)【设计模式简述--第二部分(单例模式)】
原文链接: http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part-2-singleton/ De ...
- C#+JQuery+.Ashx+百度Echarts实现全国省市地图和饼状图动态数据图形报表的统计
在目前的一个项目中,需要用到报表表现数据,这些数据有多个维度,需要同时表现出来,同时可能会有大量数据呈现的需求,经过几轮挑选,最终选择了百度的echarts作为报表基础类库.echarts功能强大,界 ...
- VS2013设置护眼背景颜色
打开VS2013 —> 工具 —> 选项 —> 环境 —> 字体和颜色 —> 选择显示项中的纯文本 —> 项目背景 —> 自定义—> 色调位85.饱和度 ...
- 从View向Controller传递复杂类型Json
mvc给我们提供多种controller中读取view数据的方法 1.从Ruquest["name"]中直接读取 2.将表单中name名称直接写在Action的参数列表中 3.将表 ...