Recently I have been studying MongoDB. I have learned a lot about data modification and complicated commands. So I will summarize some frequently used modification commands in this blog to facilitate future study.
Recently I have been studying MongoDB. I have learned a lot about data modification and complicated commands. So I will summarize some frequently used modification commands in this blog to facilitate future study.
1. Preface
Recently, I have been studying MongoDB. I have learned a lot about data modification and complicated commands. So I will summarize some frequently used modification commands in this blog to facilitate future study.
2. Command Summary
1). insert ()
Db. collection. insert (x) x is the object to be updated. It can only be a single record, for example:
Db. collection. insert ({_ id: 1, name: "test", count: 1 })
When batch insert is required, you can use the for Loop in shell, for example:
For (var I = 0; I <16; I ++ ){
Db. mytest. insert ({_ id: I, name: "test" + I, count: I })
}
If you use the find () command to query the inserted data, the result is as follows:
> Db. mytest. find ()
{"_ Id": 0, "name": "test0", "count": 0}
{"_ Id": 1, "name": "test1", "count": 1}
{"_ Id": 2, "name": "test2", "count": 2}
{"_ Id": 3, "name": "test3", "count": 3}
{"_ Id": 4, "name": "test4", "count": 4}
{"_ Id": 5, "name": "test5", "count": 5}
{"_ Id": 6, "name": "test6", "count": 6}
{"_ Id": 7, "name": "test7", "count": 7}
{"_ Id": 8, "name": "test8", "count": 8}
{"_ Id": 9, "name": "test9", "count": 9}
{"_ Id": 10, "name": "test10", "count": 10}
{"_ Id": 11, "name": "test11", "count": 11}
{"_ Id": 12, "name": "test12", "count": 12}
{"_ Id": 13, "name": "test13", "count": 13}
{"_ Id": 14, "name": "test14", "count": 14}
{"_ Id": 15, "name": "test15", "count": 15}
2). update ()
The four parameters db. collection. update (criteria, objNew, upsert, and multi) are described as follows:
Criteria: Query condition for update, similar to
ObjNew: The update object and some updated operators (such as $, $ inc...) can also be understood as
Upsert: this parameter indicates whether to insert objNew if no update record exists. true indicates insertion. The default value is false.
Multi: the default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all the records identified by the condition are updated.
Several query examples are as follows:
Db. mytest. update ({count: {$ gt: 1 }},{ $ set: {name: "OK"}) only updates the first record
Db. mytest. update ({count: {$ gt: 3 }},{ $ set: {name: "OK" }}, false, true) all updates greater than 3
Db. mytest. update ({count: {$ gt: 4 }},{ $ set: {name: "ok123" }}, true, false) only updates one
Db. mytest. update ({count: {$ gt: 6 }},{ $ set: {name: "ok123" }}, true, true) all updates greater than 6
3). save ()
Db. collection. save (x) x is the object to be inserted. The effect is the same as that of the preceding insert command. The difference between save and insert is as follows:
During data insertion, if _ id is the same, the save operation is completed and the insert operation is saved. If _ id is the same, the save operation is equivalent to the update operation.
Below are some MongoDB update Operators
For more details, please continue to read the highlights on the next page:
MongoDB details: click here
MongoDB: click here
Recommended reading:
Java-based self-growth field in MongoDB
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
How to create a new database and set in MongoDB
MongoDB beginners must read (both concepts and practices)
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]