以博文与评论为例,博文有标题内容,对应多个评论,评论有评论人、评论内容等。

()插入一条博文:
db.blog.insert(
{'_id':'','title':'this is blog title1','content':'this is blog content1'}
) ()更新一条博文
db.blog.update(
{'_id':''},
{$set:{'title':'this is blog title2','content':'this is blog content2'}}
) ()更新一条博文,如果不存在就插入
db.blog.update(
{'_id':''},
{$set:{'title':'this is blog title4','content':'this is blog content4'}},
{upsert:true}
) ()对博文增加一条评论内容
db.blog.update(
{'_id':''},
{$push:{'comments':{'user':'user1','content':'评论1'}}}
) ()根据条件删除博文的评论
db.blog.update(
{'_id':''},
{$pull:{'comments':{'user':'user1'}}}
) ()使用$addToSet避免添加重复数据
db.blog.update(
{'_id':''},
{$addToSet:{'comments':{'user':'user1','content':'评论1'}}}
) ()用$addToSet & $each联合操作批量插入数据
db.blog.update(
{'_id':''},
{$addToSet:{'comments':{'$each':[
{'user':'user1','content':'评论1'},
{'user':'user2','content':'评论2'},
{'user':'user3','content':'评论3'},
]}}}
)

MongoDB insert/update/one2many案例的更多相关文章

  1. 关于MyBatis mapper的insert, update, delete返回值

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  2. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  3. 《oracle每天一练》Merge Into 语句代替Insert/Update在Oracle中的应用实战

    转载自窃破天道 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也 ...

  4. 使用Merge Into 语句实现 Insert/Update

    网址: http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html 动机: 想在Oracle中用一条SQL语句直接进行Insert ...

  5. mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干

    1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...

  6. [Hive - LanguageManual] DML: Load, Insert, Update, Delete

    LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...

  7. MongoDB的update有关问题(JAVA)——如何一次更新所有的相同记录

    MongoDB的update问题(JAVA)——怎么一次更新所有的相同记录用如下这个函数:public WriteResult update(DBObject q,  DBObject o,  boo ...

  8. insert update delete 语法 以及用法

    insert update delete 被称为 数据定义语句语句 也就是数据的增加 修改 删除 其中不包括查询 譬如: create database -创建数据库 alter database - ...

  9. Merge Into 语句代替Insert/Update在Oracle中的应用实战

    动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录 ...

随机推荐

  1. LeetCode-Add Two Binary

    Add BinaryApr 2 '12 3558 / 10570 Given two binary strings, return their sum (also a binary string). ...

  2. 支付宝开通海外退税 阿里腾讯暗战跨境O2O_21世纪网

    支付宝开通海外退税 阿里腾讯暗战跨境O2O_21世纪网 支付宝开通海外退税 阿里腾讯暗战跨境O2O

  3. Java 8 Lambda 揭秘

    再了解了Java 8 Lambda的一些基本概念和应用后, 我们会有这样的一个问题: Lambda表达式被编译成了什么?. 这是一个有趣的问题,涉及到JDK的具体的实现. 本文将介绍OpenJDK对L ...

  4. jQuery中bind,live,delegate与one方法的用法及区别解析

    bind( )方法用于将一个处理程序附加到每个匹配元素的事件上并返回jQuery对象. .bind(eventType[, evnetData], Handler(eventObject)) 其中,参 ...

  5. KVM几种缓存模式

    原文在这里: http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/index.jsp?topic=%2Fliaat%2Fliaatbpkvmguestca ...

  6. win7安装ruby on rails

    开发机:win7 旗舰版 - 64位 1,安装ruby,下载rubyinstaller-2.0.0-p451.exe 下载地址:http://rubyinstaller.org/downloads/ ...

  7. 用js实现的刷新页面

    一.先来看一个简单的例子: 下面以三个页面分别命名为frame.html.top.html.bottom.html为例来具体说明如何做. frame.html 由上(top.html)下(bottom ...

  8. Java并发编程:线程和进程的创建(转)

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  9. Micosoft.ReportViewer.WebForms v 11.0... 1.0.1

    his dll is required for the use of Microsoft's forms based ReportViewer control. This version is to ...

  10. POJ1595_Prime Cuts【素数】【水题】

    Prime Cuts Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 10464Accepted: 3994 Description ...