Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

MongoDB(7)- 文档插入操作

MongoDB(7)- 文本文档插进实际操作

插入方法

db.collection.insertOne() 插进一条文本文档到结合中
db.collection.insertMany() 插进好几条文本文档到结合中
db.collection.insert() 插进一条或好几条文本文档到结合中

 

insertOne() 英语的语法文件格式

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)

只有传一个文本文档,不可以是二维数组

 

insertMany() 英语的语法文件格式

db.collection.insertMany(
   [ <document 1> , <document 2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>
   }
)

必传一个二维数组,即便 是空二维数组

 

insert() 英语的语法文件格式

db.collection.insert(
   <document or array of documents>,
   {
     writeConcern: <document>,
     ordered: <boolean>
   }
)

能够宣传单条文本文档或是文本文档二维数组

 

writeConcern

看见是一种错误捕获体制,弄清楚要干什么再升级吧

 

ordered

  • true:对二维数组中的文本文档实行井然有序插进,在其中一个文本文档产生不正确,MongoDB 将回到而不解决二维数组中的其他文本文档(默认设置)
  • false:混乱插进,在其中一个文本文档产生不正确,则再次解决二维数组中的别的文本文档

 

三种 insert 方式 的回到內容

// 插进一条文本文档
> db.test.insert({})
WriteResult({ "nInserted" : 1 })

// 插进好几条文本文档
> db.test.insert([])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})

 

插进实际操作的关键专业知识

  • MongoDB 向结合里插进纪录时,不必事前对数据储存构造开展界定,每一个文本文档的算法设计都能够是不一样的
  • 假如待插进的结合不会有,则插进实际操作会默认设置建立结合
  • MongoDB 中,插进实际操作以单独结合为总体目标
  • MongoDB 中的全部载入实际操作全是单独文本文档等级的原子操作

 

插进不特定 _id 字段名的文本文档

db.test.insert( { item : "card", qty : 15 })

 

MongoDB 会全自动给它分派一个 _id

db.test.find()
{ "_id" : ObjectId("60b4e2eeec0fd33d89e97a98"), "item" : "card", "qty" : 15 }

这种 Objectld 值与实行实际操作时的设备和時间相关

 

插进特定 _id 字段名的文本文档

值 _id 务必在结合中唯一,以防止反复键不正确

db.test.insert(
    { _id: 10, item: "box", qty: 20 }
)

db.test.find()
{ "_id" : 10, "item" : "box" , "qty": 20

能够见到新插进文本文档的 id 数值设定的 id 值

 

插进文本文档二维数组

插进的好几个文本文档不必具备同样的字段名

db.test1.insert(
    [
        { _id: 11, item: "pencil", qty: 50, type: "no.2" },
        { item: "pen", qty: 20 },
        { item: "eraser", qty: 25 }
    ]
)
  • 的第一个文本文档包括一个 _id 字段名和一个 type 字段名
  • 第二个和第三个文本文档不包含 _id 字段名
  • 因而,在插进全过程中,MongoDB 可能为第二个和第三个文本文档建立默认设置 _id 字段名
db.test1.find()
{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }
{ "_id" : ObjectId("60b4e98fec0fd33d89e97a99"), "item" : "pen", "qty" : 20 }
{ "_id" : ObjectId("60b4e98fec0fd33d89e97a9a"), "item" : "eraser", "qty" : 25 }

 

混乱插进

db.products.insert(
   [
     { _id: 20, item: "lamp", qty: 50, type: "desk" },
     { _id: 21, item: "lamp", qty: 20, type: "floor" },
     { _id: 22, item: "bulk", qty: 100 }
   ],
   { ordered: false }
)

假如在插进在其中一个文本文档期内产生不正确,MongoDB 会再次将其他文本文档插进二维数组中

 

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
未整理

【系统提升】学地道英文读商业秘籍

2021-5-31 0:00:00

未整理

乘风广告联盟系统 v6.7

2021-6-2 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索