上一篇文章中介绍了MongoDB的一些基本知识,同时看到了怎么启动一个MongoDB服务,并且通过MongoDB自带的shell工具连接到了服务器。

这一次,就通过MongoDB shell介绍一下对文档的增删改操作。

增加新文档

接着上一篇文章,打开一个MongoDB shell。

通过“show dbs”可以看到所有的数据库。然后我们通过“use blog”切换到blog数据库来开始下面的演示。

使用“db”命令显示当前正在使用的数据库。

 1 C:\mongodb\bin>mongo.exe
2 MongoDB shell version: 2.4.6
3 connecting to: test
4 > show dbs
5 local 0.078125GB
6 >
7 > use blog
8 switched to db blog
9 >
10 > db
11 blog
12 >

接下来就开始新建文档,插入文档的操作了

 > post1 = {"title":"learn MongoDB", "author":"Wilber", "date":new Date()}
{
"title" : "learn MongoDB",
"author" : "Wilber",
"date" : ISODate("2014-11-29T06:19:32.556Z")
}
> db.blog.posts.insert(post1)
> db.blog.posts.find()
{ "_id" : ObjectId("5479657a421b7f1536cfb1f7"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T06::.556Z") }
> show collections
blog.posts
system.indexes
>

通过相同的方式插入新的文档。

post2 = {"title":"learn English", "author":"Will", "date":new Date()}
post3 = {"title":"learn C#", "author":"Li", "date":new Date()}
post4 = {"title":"learn SQL", "author":"July", "date":new Date()}

删除文档

通过remove操作,可以对文档进行删除。如果没有任何参数,remove将会删除collection中所有的文档,而且删除操作是不可逆的,所以要小心操作。

 > db.blog.posts.remove()
> db.blog.posts.find()
>

同时,remove操作可以支持条件删除。

 > db.blog.posts.find()
{ "_id" : ObjectId("547967c1421b7f1536cfb1fe"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T06::.318Z") }
{ "_id" : ObjectId("547967c1421b7f1536cfb1ff"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
29T06::.349Z") }
{ "_id" : ObjectId("547967c1421b7f1536cfb200"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T06:2
:.365Z") }
{ "_id" : ObjectId("547967c2421b7f1536cfb201"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T0
::.380Z") }
>
>
> db.blog.posts.remove({"author":"Will"})
> db.blog.posts.find()
{ "_id" : ObjectId("547967c1421b7f1536cfb1fe"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1
-29T06::.318Z") }
{ "_id" : ObjectId("547967c1421b7f1536cfb200"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T06:2
:.365Z") }
{ "_id" : ObjectId("547967c2421b7f1536cfb201"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T0
::.380Z") }
>

更新文档

可以通过update方法更新数据库中的文档,update有两个参数,一个是查询文档,用来找出要更新的文档,另一个是修改器文档,描述多找到的文档进行怎样的更新。

文档的更新可以分为两种:文档替换和使用修改器更新

文档替换

当文档的模式变化很大的时候,一般都是采用文档替换的方式进行文档更新。

 > post1.detailInfo = {"author":"Wilber","description":"this is a post about MongoDB"}
{ "author" : "Wilber", "description" : "this is a post about MongoDB" }
> delete post1.author
true
> db.blog.posts.update({"author":"Wilber"}, post1)
> db.blog.posts.find({"detailInfo.author":"Wilber"})
{ "_id" : ObjectId("547971c5421b7f1536cfb202"), "title" : "learn MongoDB", "date" : ISODate("2014-11-29T07:12:05.560Z"),
"detailInfo" : { "author" : "Wilber", "description" : "this is a post about MongoDB" } }
>

修改器更新

对于只需要部分更新的文档,通过修改器更新会很方便。直接上例子。

$set: 修改文档的一个指定键的值,如果没有则创建。

 > db.blog.posts.update({"author":"Li"}, {$set: {"title":"how to learn C#"}})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "author" : "Li", "date" : ISODate("2014-11-29T07:12:05.591Z"), "title" :
"how to learn C#" }
> db.blog.posts.update({"author":"Li"}, {$set: {"age":}})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : , "author" : "Li", "date" : ISODate("2014-11-29T07:12:05.591Z"
), "title" : "how to learn C#" }
>

$addToSet, $push, $pop:这三个都是对数组的操作,$addToSet能够避免重复添加

 > comments = []
[ ]
> comment0 = {"score": , "content":"just so so"}
{ "score" : , "content" : "just so so" }
> comment1 = {"score": , "content":"very good"}
{ "score" : , "content" : "very good" }
>
> db.blog.posts.update({"author":"Li"}, {$set: {"comments": comments}})
> db.blog.posts.update({"author":"Li"}, {$set: {"comments.0": comment0}})
> db.blog.posts.update({"author":"Li"}, {$set: {"comments.1": comment1}})
> db.blog.posts.update({"author":"Li"}, {$addToSet: {"comments": comment1}})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : , "author" : "Li", "comments" : [ { "score" : ,
"content" : "just so so" }, { "score" : , "content" : "very good" } ], "date" : ISODate("2014-11-29T07:12:
.591Z"), "title" : "how to learn C#" }
> db.blog.posts.update({"author":"Li"}, {$push: {"comments": comment1}})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : , "author" : "Li", "comments" : [ { "score" : ,
"content" : "just so so" }, { "score" : , "content" : "very good" }, { "score" : , "content
" : "very good" } ], "date" : ISODate("--29T07::.591Z"), "title" : "how to learn C#" }
> db.blog.posts.update({"author":"Li"}, {$pop: {"comments": comment1}})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : , "author" : "Li", "comments" : [ { "score" : ,
"content" : "just so so" }, { "score" : , "content" : "very good" } ], "date" : ISODate("2014-11-29T07:12:
.591Z"), "title" : "how to learn C#" }
>

$inc:这个跟$set的用法类似,只不过$inc是专门针对数字操作的。

 > db.blog.posts.update({"author":"Li"}, {$inc: {"comments.1.score": }})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : , "author" : "Li", "comments" : [ { "score" : ,
"content" : "just so so" }, { "score" : , "content" : "very good" } ], "date" : ISODate("2014-11-29T07:12:
.591Z"), "title" : "how to learn C#" }
>

$unset:这个命令是用来删除指定键的。

 > db.blog.posts.update({"author":"Li"}, {$unset: {"comments": }})
> db.blog.posts.find({"author":"Li"})
{ "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : , "author" : "Li", "date" : ISODate("2014-11-29T07:12:05.591Z"
), "title" : "how to learn C#" }

update(arg1, arg2, arg3, arg4)

上面已经介绍了update的前两个参数的含义和用法,其实update命令还有两个参数。

arg3:这个参数代表upsert用法;如果这个参数设置为true,那么就会使用upsert的方式更新文档。

 > db.blog.posts.update({"author":"June"}, {"author": "June", "title": "C++ introduction"}, true)
> db.blog.posts.find({"author":"June"})
{ "_id" : ObjectId("5479781d217928d2be69552c"), "author" : "June", "title" : "C++ introduction" }
>

arg4:这个参数可以设置是否批量更新

 > db.blog.posts.insert(post1)
> db.blog.posts.find({"detailInfo.author":"Wilber"})
{ "_id" : ObjectId("547971c5421b7f1536cfb202"), "title" : "learn MongoDB", "date" : ISODate("2014-11-29T07:12:05.560Z"),
"detailInfo" : { "author" : "Wilber", "description" : "this is a post about MongoDB" } }
{ "_id" : ObjectId("54797864421b7f1536cfb206"), "title" : "learn MongoDB", "date" : ISODate("2014-11-29T07:12:05.560Z"),
"detailInfo" : { "author" : "Wilber", "description" : "this is a post about MongoDB" } }
> db.blog.posts.update({"detailInfo.author":"Wilber"}, {$set: {"title":"how to learn MongoDB"}}, false, true)
>
> db.blog.posts.find({"detailInfo.author":"Wilber"})
{ "_id" : ObjectId("54797864421b7f1536cfb206"), "date" : ISODate("2014-11-29T07:12:05.560Z"), "detailInfo" : { "author"
: "Wilber", "description" : "this is a post about MongoDB" }, "title" : "how to learn MongoDB" }
{ "_id" : ObjectId("547971c5421b7f1536cfb202"), "date" : ISODate("2014-11-29T07:12:05.560Z"), "detailInfo" : { "author"
: "Wilber", "description" : "this is a post about MongoDB" }, "title" : "how to learn MongoDB" }
>

总结

这篇文章主要介绍了通过MongoDB shell对数据库文档进行增删改操作,接下来会介绍查询的操作。

Ps: 附件包含了本文中的所有命令作为参考。

http://files.cnblogs.com/wilber2013/test.js

MongoDB文档的增删改操作的更多相关文章

  1. 对mongo文档的增删改操作

    在mongo db 中增加.删除.修改文档有好多方法,这里简单记录一下我所知道的一些方法. 前置条件: 1.创建study数据库  use study; 2.创建persons集合,当第一次向pers ...

  2. Elasticsearch之文档的增删改查以及ik分词器

    文档的增删改查 增加文档 使用elasticsearch-head查看 修改文档 使用elasticsearch-head查看 删除文档 使用elasticsearch-head查看 查看文档的三种方 ...

  3. mongodb对数组元素及内嵌文档进行增删改查操作(转)

    from:https://my.oschina.net/132722/blog/168274 比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: &l ...

  4. MongoDB对数组元素及内嵌文档进行增删改查操作

    比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: {    "_id" : "195861",    &qu ...

  5. Mongodb的基本操作-数据库 集合 文档的增删改查

    数据库操作: //查看有哪些数据库 > show dbs local  0.078GB mydb   0.078GB //use操作将切换到一个数据库 如果数据库存在将直接切换 如果不存在 那么 ...

  6. 分布式搜索elasticsearch 索引文档的增删改查 入门

    1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/ ...

  7. Java对XML文档的增删改查

    JAVA增删改查XML文件   最近总是需要进行xml的相关操作. 不免的要进行xml的读取修改等,于是上网搜索,加上自己的小改动,整合了下xml的常用操作. 读取XML配置文件 首先我们需要通过Do ...

  8. head插件对elasticsearch 索引文档的增删改查

    1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口.  RESTful接口URL的格式: http://localhost:9200 ...

  9. Elasticsearch 索引文档的增删改查

    利用Elasticsearch-head可以在界面上(http://127.0.0.1:9100/)对索引进行增删改查 1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演 ...

随机推荐

  1. 百度地图Api进阶教程-弹出信息窗口5.html

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="ini ...

  2. 【html】关于锚点的一些事

    今天修改公会系统,有用到锚点对页面位置进行控制,结果碰到了一些问题,通过查询相关资料解决了,在这里总结下. 两种方法跳转到锚点: 1.给锚点添加 name 属性和 id 属性.一般只要加 name 就 ...

  3. Centos warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

    vi /etc/environment add these lines... LANG=en_US.utf-8 LC_ALL=en_US.utf-8 QQ技术交流群:576269252 ------- ...

  4. python 核心编程 01

    特殊变量 python用下划线作为变量的前缀和后缀指定特殊变量._XXX : 不用 'from module import *' 导入, 可以认为是模块中的私有变量__XXX__ : 系统定义的名字_ ...

  5. 《编程之美》practice

    1.2.中国象棋将帅问题 要求:只用一个字节存储变量,输出将帅不照面的所有可能位置. 思路简单,就是穷举让将和帅不在同一列即可,用char高四字节和低四字节分别存储将和帅的位置,位置编号从1到9.代码 ...

  6. ViewPager一屏显示多个item,及边缘滑动事件优化

    关于ViewPager显示两边的item方法,网络上是方法都在ViewPager外包一个Layout, 然后设置ViewPager和外面的Layout的clipChildren="false ...

  7. R语言 使用命令行参数运行R程序

    args_test.R 代码如下: Args <- commandArgs()cat("Args[1]=",Args[1],"\n")cat(" ...

  8. Qt封装QTcpServer参考资料--QT自带QTcpServer架构分析

    Qt 4.6自带的threaddedfortuneserver是个简单明了的 Qt  C/S网络编程server端程序的例子, 该例子演示了 QTcpServer与QThread配合的方法. 代码不多 ...

  9. strcpy_s与strcpy的区别

    strcpy_s和strcpy()函数的功能几乎是一样的.];];strcpy_s(str1,,;}#include<iostream>  #include<]; ]; strcpy ...

  10. Numpy 的数组转置和轴对换

    数组转置 转置(transpose)是重塑的一种特殊形式, 它返回的是源数据的视图(不会进行任何操作.)数组不仅有transpose,还要特殊的T属性 计算矩阵内积 高维数组transpose 详细讲 ...