• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >MongoDB > MongoDB快速入门笔记(三)之MongoDB插入文档操作

MongoDB快速入门笔记(三)之MongoDB插入文档操作

作者:tiger_zhang 字体:[增加 减小] 来源:互联网 时间:2017-05-11

tiger_zhang通过本文主要向大家介绍了mongodb文档,mongodb中文文档,mongodb官方文档,mongodb帮助文档,mongodb 删除文档等相关知识,希望本文的分享对您有所帮助

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

本文给大家介绍MongoDB的插入文档的方法,一起看看吧

1、文档的数据存储格式为BSON,类似于JSON。MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成。

shell操作有insert和save两种方法。当插入一条数据有“_id”值,并且现在集合中已经有相同的值,使用insert插入时插入不进去,使用save时,会更新数据。

> db.student.drop()
true
> db.student.insert({"_id": 1, "name":"zhangsan", "age": 28})
WriteResult({ "nInserted" : 1 })
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
> db.student.insert({"_id": 1, "name":"zhangsan", "age": 27})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: zyhdb.student index: _id_ dup key: { : 1.0 }"
}
})
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
> db.student.save({"_id": 1, "name":"zhangsan", "age": 27})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 } 
</div>

2、批量插入,网上的文档都说不能MongoDB不支持批量插入,现在试过可以,应该是目前的版本支持批量插入了。

> db.student.insert([{"_id": 2, "name": "lisi"},{"_id": 3, "name": "wangwu"}, {"_id": 4, "name": "zhaoliu", "age": 28}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 }
{ "_id" : , "name" : "lisi" }
{ "_id" : , "name" : "wangwu" }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 } 
</div>

3、循环插入:

> for(var i=; i<; i++){db.fortest.insert({num: i})}
WriteResult({ "nInserted" : })
> db.fortest.find()
{ "_id" : ObjectId("eceadaeabab"), "num" : 0}
{ "_id" : ObjectId("eceadaeabab"), "num" : 1}
{ "_id" : ObjectId("eceadaeabab"), "num" : 2}
{ "_id" : ObjectId("eceadaeabab"), "num" : 3}
{ "_id" : ObjectId("eceadaeabab"), "num" : 4}
{ "_id" : ObjectId("eceadaeababa"), "num" : 5}
{ "_id" : ObjectId("eceadaeababb"), "num" : 6}
{ "_id" : ObjectId("eceadaeababc"), "num" : 7}
{ "_id" : ObjectId("eceadaeababd"), "num" : 8}
{ "_id" : ObjectId("eceadaeababe"), "num" : 9}
</div>

以上所述是小编给大家介绍的MongoDB快速入门笔记(三)之MongoDB插入文档操作的相关知识,希望对大家有所帮助,更多精彩内容,敬请关注网站!

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MongoDB插入、更新、删除文档实现代码
  • MongoDB中对文档的增删查改基本操作方法总结
  • MongoDB快速入门笔记(三)之MongoDB插入文档操作
  • MongoDB快速入门笔记(四)之MongoDB查询文档操作实例代码
  • MongoDB快速入门笔记(六)之MongoDB的文档修改操作
  • MongoDB快速入门笔记(六)之MongoDB删除文档操作
  • MongoDB修改、删除文档的域属性实例

相关文章

  • 2017-05-11mongodb replica set 添加删除节点的2种方法
  • 2017-05-11MongoDB学习笔记之GridFS使用介绍
  • 2017-05-11给MongoDB添加用户权限方法分享
  • 2017-05-11MongoDB与MySQL的操作对比表及区别介绍
  • 2017-05-11MongoDB安装到windows服务的方法及遇到问题的完美解决方案
  • 2017-05-11Mongo管理用户相关操作总结
  • 2017-05-11MongoDB教程之数据操作实例
  • 2017-05-11MongoDB学习笔记之分组(group)使用示例
  • 2017-05-11MongoDB查询性能优化验证及验证
  • 2017-05-11MongoDB远程访问配置步骤详解

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • MongoDB安全配置详解
    • mongoDB 实现主从读写分离实现的实例代码
    • MongoDB系列教程(七):MongoDb数据结构详解
    • Mongodb索引的优化
    • Windows下把MongoDB安装为系统服务的方法
    • MongoDB入门教程之分片技术详解
    • MongoDB的基础知识简介
    • 浅谈mongodb中query查询
    • MongoDB使用指南--基本操作
    • JavaScript按日期查询MongoDB中的数据的要点示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有