手机
当前位置:查字典教程网 >编程开发 >MongoDB >MongoDB快速入门笔记(三)之MongoDB插入文档操作
MongoDB快速入门笔记(三)之MongoDB插入文档操作
摘要:MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。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 }

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 }

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}

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

【MongoDB快速入门笔记(三)之MongoDB插入文档操作】相关文章:

MongoDB系列教程(六):java操作mongodb实例

MongoDB与MySQL的操作对比表及区别介绍

mongoDB分页的两种方法(图例)

MongoDB aggregate 运用篇个人总结

MongoDB插入数据的3种方法

关于NoSQL之MongoDB的一些总结

MongoDB入门教程之细说MongoDB数据库的增删查改操作

MongoDB系列教程(二):MongoDB简介

MongoDB 快速入门

MongoDB教程之查询操作实例

精品推荐
分类导航