MongoDB Learning (2): common modification operations and mongodb learning and Modification
Insert document (insert database)
Db. person. insert ({_ id: "0001", name "yuexin "})
Clear Data
Db. person. drop ()
Batch insert documents
Batch insert is not supported in shell.
Batch insert using for Loop
For (var I = 0; I <10; I ++ ){
.. Db. persons. insert ({_ id: I, name: "yuexin" + I })
..}
Save operation
The difference between the save operation and the insert operation is that when the id is the same, the save operation changes to the update operation, and the insert operation reports an error.
Delete all data in the list
Db. persons. remove () delete data in persons but do not delete the index (db. system. indexes. find () has a value)
For
Db. person. drop () will delete the index
Delete
Db. persons. remove ({_ id: "3 "})
If you want to clear a collection with a large amount of data, it is much more efficient to directly Delete the set and re-index it.
1. Tough Document Replacement update operations
Update
> Db. persons. update ({age: 55 },{ name: "000 "})
2. When a primary key conflict occurs, an error is reported and the update operation is stopped.
An error is reported when a document with a hard replacement conflict with an existing document id.
3. insertOrUpdate operation
Objective: To update the data queried by the queryer and replace the data when the data is not found.
Practice:> db. persons. update ({name: "0004" },{ name: "1114"}, true)
4. Batch update
> Db. persons. update ({name: "yuexin" },{$ set: {name: "text" }}, false, true)
By default, the first data entry is modified by default when the queryserver detects multiple data entries.
Db. [documentName]. update ({queryer}, {modifier}, false, true)
Modifier:
$ Set is used to specify a key-value pair. If it exists, it is modified. If it does not exist, it is added.
$ Inc is only used for numeric types. It can add or subtract numeric values corresponding to the specified key.
> Db. persons. update ({age: 20}, {$ inc: {age:-2 }})
$ Unset Delete the specified key
> Db. persons. update ({age: 18}, {$ unset: {age: 1 }})
$ Push
1. If the specified key is a new value appended to the array
> Db. persons. insert ({_ id: 0, name: 0, book: []})
> Db. persons. find ()
{"_ Id": 0, "name": 0, "book": []}
> Db. persons. update ({name: 0}, {$ push: {book: "abc "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc"], "name": 0}
> Db. persons. update ({name: 0}, {$ push: {book: "abcd "}})
> Db. persons. update ({name: 0}, {$ push: {book: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc", "abcd", "abcd"], "name": 0}
2. If the specified key is not an array, the current operation is interrupted.
Cannot apply $ push/$ pushAll modifier to non-array
3. If the specified key does not exist, create an array-type key-value pair.
> Db. persons. update ({name: 0}, {$ push: {things: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc", "abcd", "abcd"], "name": 0, "things": ["abcd"
]}
$ PushAll is similar to push. It is used to add array data in batches.
> Db. persons. update ({name: 0}, {$ pushAll: {things: ["abcd", "01", "02"]})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc", "abcd", "abcd"], "name": 0, "things": ["abcd"
, "Abcd", "01", "02"]}
$ AddToSet if this option exists in the target array, no operation is performed. If this option does not exist, add it.
> Db. persons. insert ({_ id: 0, name: 0, book: []})
> Db. persons. update ({name: 0}, {$ addToSet: {book: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abcd"], "name": 0}
> Db. persons. update ({name: 0}, {$ addToSet: {book: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abcd"], "name": 0}
$ Pop: Delete the first or last one (-1 is the first and 1 is the last one)
> Db. persons. update ({name: 0}, {$ pop: {book:-1 }})
$ Pull delete a specified
> Db. persons. update ({name: 0}, {$ pull: {book: "01 "}})
$ Pull Delete multiple
> Db. persons. update ({name: 0}, {$ pullAll: {book: ["01", "02"]})
$ Array positioner. If the array has multiple values, we only want to operate some of them. We need to use the locator ($ )(??)
The modifier is placed on the outermost layer, and the queryer is placed on the inner layer.
$ AddToSet combined with $ each to complete batch array update
If yes, no more
> Db. persons. find ()
{"_ Id": 0, "book": ["js"], "name": 0}
> Db. persons. update ({_ id: 0}, {$ addToSet: {book :{$ each: ["js", "db"] }}})
> Db. persons. find ()
{"_ Id": 0, "book": ["js", "db"], "name": 0}
When a document is created, mongoDB allocates memory and reserved memory for it. When the modification operation does not exceed the reserved memory, it is very fast. If the modification operation does not exceed the reserved memory, it will consume time.
RunCommand can execute special functions in mongoDB.
FindAndModify is one of the special functions. It is used to return the document after update or remove.
> Db. persons. find ()
{"_ Id": 0, "book": ["js", "db"], "name": 0}
> Ps = db. runCommand ({
... "FindAndModify": "persons ",
... "Query": {name: 0 },
... "Update": {$ set: {age: 100 }},
... New: true })
{
"LastErrorObject ":{
"UpdatedExisting": true,
"N": 1,
"ConnectionId": 1,
"Err": null,
"OK": 1
},
"Value ":{
"_ Id": 0,
"Age": 100,
"Book ":[
"Js ",
"Db"
],
"Name": 0
},
"OK": 1
}
> Ps. value
{& Quot; _ id & quot;: 0, & quot; age & quot;: 100, & quot; book & quot;: [& quot; js & quot;, & quot; db & quot;], & quot; name & quot;: 0}
How to change mongodb in java
Java-based mongodb insert, read, modify, and delete operations
This article describes how to operate MongoDB in Java and how to perform routine database operations in MongoDB. The content of this article is as follows: Step 1: Install MongoDB requires no complicated steps. You only need to check the installation instructions on the official MongoDB website and select an appropriate version based on your operating system. Step 2: Start the MongoDB server. Run the bin folder in mongod.exe (I am using Windows OS) and start the MongoDB server. By default, the server will start port 27017. You need to create and store the data in the/data/db directory during installation. Step 3: Start MongoDB shell you can start MongoBD shell by running the cmd.exe file. Step 4: Use MongoDB to create a database use MongoDB to input the following content in MongoDB shell to create a database named "company. Use company remember that unless you save something in MangoDB, it will not automatically save for you. Use the following command to view available databases. It will show that "company" has not been created. Show dbs; Step 5: save data in MongoDB use the following command to save the employee data to represent a collection and name it employees. Employee = {name: "A", no: 1} db. employees. save (employee) use the following command to view the data in the collection. Db. users. find (); how to operate MongoDB using Java? The following is a simple Java code. You can obtain the mongo-java driver here. It is very simple. You only need to use the following code and repeat the above operations. Package com. eviac. blog. mongo; import java.net. unknownHostException; import com. mongodb. basicDBObject; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. DBCursor; import com. mongodb. mongo; import com. mongodb. except exception; public class MongoDBClient {public static void main (String [] args) {try {Mongo mongo = new Mongo ("localhost", 27017); DB db = mongo. getDB ("company"); DBCollection collection = db. getCollection ("employees"); BasicDBObject employee = new BasicDBObject (); employee. put ("name", "hanhannah"); employee. put ("no", 2); collection. insert (employee); BasicDBObject searchEmployee = new BasicDBObject (); searchEmployee. put ("no & quot ...... remaining full text>
Who has learned the MongoDB video tutorial?
I recommend a document called "in-depth development of MongoDB applications (basic, Development Guide, system management, cluster and system architecture)" with 22 lessons, it focuses on explaining the common features and advanced features of MongoDB, and comprehensively and deeply analyzes MongoDB from the perspective of actual development. For details, contact me at 1511065175.
MongoDB basics:
Lecture 1: nosql and MongoDB (background of the rise of nosql, Introduction to various nosql databases, and features of MongoDB)
Section 2: MongoDB installation and configuration (MongoDB installation and use, basic system management skills, and web Console usage)
Lecture 3: MongoDB shell (introduces the usage and commands of MongoDB shell, Backup recovery, data import and export)
Lecture 4: Concepts of MongoDB documents, collections, and databases (Introduction to documents, collections, databases, and other basic concepts, database file storage methods, and command Rules)
Lecture 5: Introduction to Mongodb data types (details on MongoDB supported data types)
MongoDB Development Guide:
Lecture 6: MongoDB add, delete, and modify documents (describes commands for adding, deleting, and modifying documents in MongoDB, insertion principle, batch modification, and modifier usage)
Lecture 7: MongoDB query syntax 1 (describes in detail the powerful query functions of MongoDB, and queries by operators such as $ in, $ or, $ ne, $ lt, and $ gt)
Lecture 8: MongoDB query syntax 2 (describes in detail the powerful query functions of MongoDB, such as regular expression query, array query, and embedded document query)
Lecture 9: MongoDB query syntax 3 (detailed description of MongoDB where queries, cursor operations, paging queries, code examples, and cursor details)
Lecture 10: MongoDB index (detailed description of MongoDB index principles, management, index query and analysis tools, and mandatory index usage)
11th Lecture: MongoDB aggregation statistics (describes the MongoDB aggregation statistics function)
Lecture 12th: MongoDB advanced guide-how commands work (describes how database commands work)
Lecture 13th: MongoDB advanced guide-fixed set and GridFS (introducing fixed set and GridFS principles and applications)
Lecture 14th: apsaradb for MongoDB advanced guide-server scripts (Introduction to server scripts dbeval and javascript storage)
MongoDB System Management:
Lecture 15th: Advanced MongoDB System Management Skills 1 (System Monitoring)
Lecture 16th: apsaradb for MongoDB advanced system management skills 2 (Database Security, backup and recovery, and data restoration)
MongoDB cluster and system architecture:
Lecture 17th: MongoDB replication function (describes in detail how to create, manage, and maintain MongoDB master-slave replication)
Lecture 18th: MongoDB replica set function (describes in detail how to create, manage, and maintain MongoDB replica sets)
19th Lecture: MongoDB sharding function (details on MongoDB sharding creation, management, and maintenance)
20th: MongoDB insider (in-depth analysis of MongoDB System Architecture and Data File structure principles)
MongoDB application case:
21st Lecture: Development Based on MongoDB General Account Management System 1
22nd Lecture: Development Based on MongoDB General Account Management System 2