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 ...
随机推荐
- hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系
前面几篇都是介绍的用配置文件来实现实体类到数据库表的映射,这种方式是比较麻烦的,每一个pojo类都需要写一个相应的*.hbm.xml,无疑增加了很多代码量,不过也有优点就是利于维护,为了方便开发,Hi ...
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...
- WebGIS开源方案中空间数据的入库、编辑、发布的操作流程
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 本开源方案的构架是:geoserver(服务器)+tomca ...
- Linux服务器(Ubuntu14.04)添加远程连接VNC Server
1.打开终端输入:sudo apt-get install xrdp, 2. sudo apt-get install vnc4server , 3. sudo apt-get install ...
- Handle类与线程
首先声明Handle对象和该类的handleMessage方法: Handler BarHandler = new Handler(){ @Override public void handleMes ...
- Moon.Orm常见问题问答FAQ
有问题在评论,我看到邮件会尽快回复 1.重点了解Db里面的方法.这是核心. 2.关于查询语句MQL:http://www.cnblogs.com/humble/p/3380065.html 3.关于如 ...
- ros::spin() 和 ros::spinOnce() 区别及详解
版权声明:本文为博主原创文章,转载请标明出处: http://www.cnblogs.com/liu-fa/p/5925381.html 博主提示:本文基于ROS Kinetic Kame,如有更(g ...
- web性能优化:详说浏览器缓存
TOC 背景 浏览器的总流程图 一步一步说缓存 朴素的静态服务器 设置缓存超时时间 html5 Application Cache Last-Modified/If-Modified-Since Et ...
- C#冒泡排序法
基本原理 比较两个相邻的数的大小,每次比较完后把最大的数放到本轮的末尾.假设有数组: 258,445,131,97,22,36,17,38,28,28,第一轮:258和445比较,位置不用交换.第二轮 ...
- csharp: MySQL Stored Procedure using DAL
# 建表 塗聚文 20160907 drop table attendrecord; create table attendrecord ( seq INT NOT NULL PRIMARY KEY ...