mongodb增删改查之增

2,293次阅读
没有评论

共计 916 个字符,预计需要花费 3 分钟才能阅读完成。

mongodb增删改查之增

最早有一篇文章记录了如何安装mongodb,现在开始学习,发现看了一遍书立马就就忘了,还是自己动手操作一下

首先开始增删改查吧!

目前使用的软件是studio3t,用的感觉还不错

不废话介入正题了。。。。。

涉及到插入会有三个方法

db.collection.insert() Creates a new document in a collection.
db.collection.insertOne() Inserts a new document in a collection.
db.collection.insertMany() Inserts several new document in a collection.

官网里面说明了insert是可以直接插入一个文档或者多个文档,那么为啥还有下面两个?

insertOne 和 insertMany()分别是插入一个和多个

若要从返回值上来看的化 insert 返回插入的读写结果返回,

var ss=db.test.insert({"bar":"test"})
ss

WriteResult({ "nInserted" : 1 })

其余两个操作与此不同

var ss=db.test.insertOne({"bar":"test"})
ss
{ 
    "acknowledged" : true, 
    "insertedId" : ObjectId("5afd254e2c63ae1a943bdb24")
}

这里有个原因要说明:
在大部分的驱动中insert()被丢弃,建议使用其余两个,想看英文原文下面也给出了
The insert() method is deprecated in major driver so you should use the the .insertOne() method whenever you want to insert a single document into your collection and the .insertMany when you want to insert multiple documents into your collection.

insertMany

使用方法,可以向collection中插入列表数据

db.test.insertMany([{"a":"b"},{"c":"d"}])
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2018-05-17发表,共计916字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码