mongodbCRUD操作-INSERT

# ./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 
 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 
 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/

一、插入文档

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

Collection Creation:

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

方法:

  • insertOne

db.collection.insertOne(
   ,
   {
      writeConcern:    //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(
   { [  , , ... ] },//An array of documents to insert into the collection. 注意是数组
   {
      writeConcern: ,
      ordered:    //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的功能
   ,  //数组或者文档都可以添加
   {
     writeConcern: ,
     ordered:  //跟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()

网页题目:mongodbCRUD操作-INSERT
URL分享:http://myzitong.com/article/gcsjcd.html