温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mongodb CRUD操作-INSERT

发布时间:2020-04-08 23:22:52 来源:网络 阅读:970 作者:会说话的鱼 栏目:MongoDB数据库

# ./mongod

2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] MongoDB starting : pid=36578 port=27017 dbpath=/data/db 64-bit host=IBG-opensource-DB-cwT13
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] db version v3.6.2
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] modules: none
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] build environment:
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten]     distmod: rhel70
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] options: {}
2018-04-15T18:52:36.354+0800 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-04-15T18:52:36.354+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7423M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-04-15T18:52:36.512+0800 I STORAGE  [initandlisten] WiredTiger message [1523789556:512963][36578:0x7f6aac8c2b00], txn-recover: Main recovery loop: starting at 2/35072
2018-04-15T18:52:36.615+0800 I STORAGE  [initandlisten] WiredTiger message [1523789556:615462][36578:0x7f6aac8c2b00], txn-recover: Recovering log 2 through 3
2018-04-15T18:52:36.676+0800 I STORAGE  [initandlisten] WiredTiger message [1523789556:676715][36578:0x7f6aac8c2b00], txn-recover: Recovering log 3 through 3
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 65535 processes, 655350 files. Number of processes should be at least 327675 : 0.5 times number of files.
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.755+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-04-15T18:52:36.756+0800 I NETWORK  [initandlisten] waiting for connections on port 27017



# ./mongo

MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
Server has startup warnings: 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 65535 processes, 655350 files. Number of processes should be at least 327675 : 0.5 times number of files.
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten]



https://docs.mongodb.com/manual/reference/method/db.collection.insert/


一、插入文档

vps2" >插入行为,,如果集合不存在,则创建

Collection Creation:

If the collection does not currently exist, insert operations will create the collection.


方法:

  • insertOne

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>   //Optional. A document expressing the write concern. Omit to use the default write concern.
   }
)

注意:

集合不存在该操作会创建集合。

该操作不支持 db.collection.explain() 查询计划 可以使用insert 代替。


_id Field:


If the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the new document.


ObjectId值由12个字节组成,其中前四个字节是反映ObjectId创建的时间戳,具体为:

a 4-byte value representing the seconds since the Unix epoch,

a 3-byte machine identifier,

a 2-byte process id, and

a 3-byte counter, starting with a random value.


 

 > db.products.insertOne( { item: "card", qty: 15 } );

{
"acknowledged" : true,         返回值
"insertedId" : ObjectId("5ad2c1572c23d0642cd35b48")
}
> db.products.find();
{ "_id" : ObjectId("5ad2c1572c23d0642cd35b48"), "item" : "card", "qty" : 15 }

 


> db.products.insertOne(

      { "item": "envelopes", "qty": 100, type: "Self-Sealing" },

       { writeConcern: { w : "majority", wtimeout : 100 } }    //w:majority,表示>1/2的节点有数据   

  )

{
"acknowledged" : true,
"insertedId" : ObjectId("5ad2cecd2c23d0642cd35b4c")
}


   

  • insertMany

db.collection.insertMany(
   { [ <document 1> , <document 2>, ... ] },//An array of documents to insert into the collection. 注意是数组
   {
      writeConcern: <document>,
      ordered: <boolean>   //Optional. A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true.   默认式按照顺序添加的    顺序添加的速度要慢于不按顺序添加的
   }
)



注意:

每一组操作最大可以有1000个文档。

顺序添加的速度要慢于不按顺序添加的。

不存在的集合会自动创建。

insertMany() 也不支持db.collection.explain() 可以使用insert 代替。

如果添加出错会报出 BulkWriteError exception 异常,按照顺序添加的 操作遇到错误会直接停止,而不按照顺序的会继续执行在队列中的写操作。


> db.products.insertMany( [                

      { item: "card", qty: 15 },

      { item: "envelope", qty: 20 },

       { item: "stamps" , qty: 30 }

      ] ,  { w: "majority", wtimeout: 100 }

  );


{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5ad2c2332c23d0642cd35b49"),
ObjectId("5ad2c2332c23d0642cd35b4a"),
ObjectId("5ad2c2332c23d0642cd35b4b")
]
}


  • insert

db.collection.insert(  #同时又insertOne 和insertMany的功能
   <document or array of documents>,  //数组或者文档都可以添加
   {
     writeConcern: <document>,
     ordered: <boolean> //跟insertMany()操作作用一致
   }
)


db.products.insert( [                

      { item: "card", qty: 15 },

      { item: "envelope", qty: 20 },

       { item: "stamps" , qty: 30 }

      ] ,  { w: "majority", wtimeout: 100 }

  );


db.products.insert( { item: "card", qty: 40 } );



Additional Methods for Inserts

when used with the upsert: true option:以下方法当没有查询条件匹配时创建新文档,默认是false。


db.collection.update()          
db.collection.updateOne()        
db.collection.updateMany()      
db.collection.findAndModify()    
db.collection.findOneAndUpdate()
db.collection.findOneAndReplace()
db.collection.save()
db.collection.bulkWrite()


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI