Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb CRUD Action-INSERT

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

#. / 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-cwT132018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] db version v3.6.22018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c5394202018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] allocator: tcmalloc2018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] modules: none2018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] build environment:2018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] distmod: rhel702018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] distarch: x86 * 642018-04-15T18:52:36.352+0800 I CONTROL [initandlisten] target_arch: x86 * 642018-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 [1523789556virtual 512963] [36578:0x7f6aac8c2b00] Txn-recover: Main recovery loop: starting at 2gamma 350722018-04-15T18:52:36.615+0800 I STORAGE [initandlisten] WiredTiger message [1523789556 15T18:52:36.615+0800 615462] [36578:0x7f6aac8c2b00], txn-recover: Recovering log 2 through 32018-04-15T18:52:36.676+0800 I STORAGE [initandlisten] WiredTiger message [1523789556 15T18:52:36.615+0800 676715] [36578:0x7f6aac8c2b00] Txn-recover: Recovering log 3 through 32018-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 to2018-04-15T18:52:36.740+0800 I CONTROL [initandlisten] * * bind to all interfaces. If this behavior is desired Start the2018-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.2connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.2Server 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 to2018-04-15T11:01:19.795+0800 I CONTROL [initandlisten] * * bind to all interfaces. If this behavior is desired Start the2018-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/

Insert a document

Vps2 "> insert behavior, or, if the collection does not exist, create

Collection Creation:

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

Methods:

InsertOne

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

Note:

The collection does not exist. This operation creates the collection.

This operation does not support the db.collection.explain () query plan. You can use insert instead.

_ id Field:

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

The ObjectId value consists of 12 bytes, of which the first four bytes reflect the timestamp created by ObjectId, as follows:

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, return value "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, which means that the node with > 1ap2 has data.

)

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

InsertMany

Db.collection.insertMany ({[,...]}, / / An array of documents to insert into the collection. Notice the array {writeConcern:, ordered: / / Optional. A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true. The default type is added more slowly in the order in which it is added than if it is not added in order})

Note:

Each set of operations can have up to 1000 documents.

The speed of sequential addition is slower than that of non-sequential addition.

Collections that do not exist are created automatically.

InsertMany () also does not support that db.collection.explain () can be replaced by insert.

If you add an error, a BulkWriteError exception exception will be reported, and the operation added in order will stop directly when it encounters an error, while the write operation in the queue will continue to be performed out of order.

> 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 (# both insertOne and insertMany functions, / / arrays or documents can be added {writeConcern:, ordered: / / consistent with insertMany () operation})

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: the following method creates a new document when no query criteria match. The default is 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 ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report