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 remove update find

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Update

The syntax is as follows:

Use the update method to update the data in the collection. Update has four parameters, the first two of which are required.

Db.person.update ({"name": "meteor"}, {"$set": {"age": 35}}, true,true)

The first parameter, the querier, locates the target document that needs to be updated (defines the matching criteria).

The second parameter: the modifier document, which specifies what to modify.

The third parameter: true means to use upsert, that is, if a document that meets the update criteria is not found, a new document will be created based on this "condition and update document". If a matching document is found, it is updated normally. Default: false

The fourth parameter: true indicates that all documents that meet the criteria will be updated. Default: false

Modifier

Common operators: $set, $inc, $push, $addToSet, $each, $pop, $pull, $unset

$set: used to specify the value of a field. If this field does not exist, create it. For updates, for documents that meet the update criteria, the fields executed by modification do not need to be fully covered.

> db.emp.find () {"_ id": 1, "ename": "tom", "age": 25, "department": "Sales", "salary": 6000} {"_ id": 2, "ename": "eric", "age": 24, "department": "HR", "salary": 4500} {"_ id": 3, "ename": "robin" Age: 30, department: Sales, salary: 8000} {"_ id": 4, "ename": "jack", "age": 28, "department": "Development", "salary": 8000} {"_ id": 5, "ename": "Mark", "age": 22, "department": "Development" "salary": 6500} {"_ id": 6, "ename": "marry", "age": 23, "department": "Planning", "salary": 5000} {"_ id": 7, "ename": "hellen", "age": 32, "department": "HR", "salary": 6000} {"_ id": 8, "ename": "sarah" "age": 24, "department": "Development", "salary": 7000} > db.emp.find (). ForEach Function (item) {... Db.emp.update ({"_ id": item._id}, {"$set": {"salary": item.salary+item.salary*.2}}, false,true). }) > db.emp.find () {"_ id": 1, "ename": "tom", "age": 25, "department": "Sales", "salary": 7200} {"_ id": 2, "ename": "eric", "age": 24, "department": "HR", "salary": 5400} {"_ id": 3, "ename": "robin" Age: 30, department: Sales, salary: 9600} {"_ id": 4, "ename": "jack", "age": 28, "department": "Development", "salary": 9600} {"_ id": 5, "ename": "Mark", "age": 22, "department": "Development" "salary": 7800} {"_ id": 6, "ename": "marry", "age": 23, "department": "Planning", "salary": 6000} {"_ id": 7, "ename": "hellen", "age": 32, "department": "HR", "salary": 7200} {"_ id": 8, "ename": "sarah" "age": 24, "department": "Development", "salary": 8400} >

$inc: used to increase the value of an existing key, or create one if the key does not exist

> db.emp.update ({}, {$inc: {"age": 2}, false,true) WriteResult ({"nMatched": 8, "nUpserted": 0, "nModified": 8}) > db.emp.find () {"_ id": 1, "ename": "tom", "age": 27, "department": "Sales", "salary": 7200} {"_ id": 2, "ename": "eric" "age": 26, "department": "HR", "salary": 5400} {"_ id": 3, "ename": "robin", "age": 32, "department": "Sales", "salary": 9600 {"_ id": 4, "ename": "jack", "age": 30, "department": "Development" "salary": 9600} {"_ id": 5, "ename": "Mark", "age": 24, "department": "Development", "salary": 7800} {"_ id": 6, "ename": "marry", "age": 25, "department": "Planning", "salary": 6000} {"_ id": 7, "ename": "hellen" "age": 34, "department": "HR", "salary": 7200} {"_ id": 8, "ename": "sarah", "age": 26, "department": "Development", "salary": 8400} > db.emp.update ({}, {$inc: {"age":-2}, false,true) WriteResult ({"nMatched": 8, "nUpserted": 0 "nModified": 8}) > db.emp.find () {"_ id": 1, "ename": "tom", "age": 25, "department": "Sales", "salary": 7200} {"_ id": 2, "ename": "eric", "age": 24, "department": "HR", "salary": 5400} {"_ id": 3 Ename: "robin", "age": 30, "department": "Sales", "salary": 9600} {"_ id": 4, "ename": "jack", "age": 28, "department": "Development", "salary": 9600} {"_ id": 5, "ename": "Mark", "age": 22, "department": "Development" "salary": 7800} {"_ id": 6, "ename": "marry", "age": 23, "department": "Planning", "salary": 6000} {"_ id": 7, "ename": "hellen", "age": 32, "department": "HR", "salary": 7200} {"_ id": 8, "ename": "sarah" "age": 24, "department": "Development", "salary": 8400} >

$push: add an element to the end of an existing array

> db.emp.update ({"ename": "sarah"}, {"$set": {"language": ["Chinese"]}}, true,true) / / A pair of documents whose name equals sarah Add an array of language WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24, "department": "Development", "salary": 8400 "language": ["Chinese"]} > db.emp.update ({"ename": "sarah"}, {"$push": {"language": "English"}, true,true) / / add a value WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8 to the end of the array. Ename: "sarah", "age": 24, "department": "Development", "salary": 8400, "language": ["Chinese", "English"]} >

$addToSet: avoid inserting duplicate values into arrays

> db.emp.update ({"ename": "sarah"}, {"$addToSet": {"language": "English"}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 0}) > db.emp.update ({"ename": "sarah"}, {"$addToSet": {"language": "Russian"}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0) "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24, "department": "Development", "salary": 8400, "language": ["Chinese", "English", "Russian"]} > db.emp.update ({"ename": "sarah"}) {"$addToSet": {"language": "Russian"}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 0}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24, "department": "Development", "salary": 8400, "language": ["Chinese", "English" "Russian"]} >

$each: combine with $push and $addToSet to add multiple values to the array at a time

> db.emp.update ({"ename": "sarah"}, {"$addToSet": {"language": {"$each": ["German", "French"]}}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24 Department: "Development", "salary": 8400, "language": ["Chinese", "English", "Russian", "German", "French"]} >

$pop: elements can be deleted from either end of the array

> db.emp.update ({"ename": "sarah"}, {"$pop": {"language": 1}, true,true) / / remove an element WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24 from the end of the array "department": "Development", "salary": 8400, "language": ["Chinese", "English", "Russian", "German"]} > db.emp.update ({"ename": "sarah"}, {"$pop": {"language":-1}}, true,true) / / remove an element WriteResult ({"nMatched": 1, "nUpserted": 0) from the header of the array "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24, "department": "Development", "salary": 8400, "language": ["English", "Russian", "German"]} >

$pull: delete the corresponding value of the array

> db.emp.update ({"ename": "sarah"}, {"$pull": {"language": "Russian"}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24, "department": "Development", "salary": 8400 "language": ["English", "German"]} > db.emp.update ({"ename": "sarah"}, {"$pull": {"language": "German"}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24 "department": "Development", "salary": 8400, "language": ["English"]}

$unset: deletes the specified field in the document

> db.emp.update ({"ename": "sarah"}, {"$unset": {"language": 1}, true,true) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.emp.find ({"ename": "sarah"}) {"_ id": 8, "ename": "sarah", "age": 24, "department": "Development", "salary": 8400} >

Eligible documents update only the first record, and if no fields are updated, add:

Db.col.update ({"age": {$gt: 25}, {$set: {"test2": "OK"}})

Eligible documents update only the first record, and if no fields are updated, ignore:

Db.col.update ({"age": {$gt: 25}}, {$inc: {"age": 1}}, false,false)

All eligible documents are updated, and if no fields are updated, add:

Db.col.update ({"age": {$gt: 25}}, {$set: {"test2": "OK"}}, false,true)

Only the first item is added to the eligible document, and if the field is not updated, add:

Db.col.update ({"age": {$gt: 25}}, {$set: {"test2": "OK"}}, true,false)

All eligible documents are added (overwriting the value of the original field):

Db.col.update ({"age": {$gt: 25}}, {$set: {"test2": "OK"}}, true,true)

Delete

Use the remove method to delete the data in the collection. It can accept a query document as an optional parameter. Given this parameter, only documents that meet the criteria can be deleted. Deleting data is permanent, cannot be undone, and cannot be restored.

Db.emp.remove ({"name": "sarah"}) / / Delete all documents in the person collection where the value of the name field is equal to ryan.

Db.emp.remove () / / Delete all documents in the person collection.

Using the drop method instead of the remove method can greatly improve the speed of deleting data. However, this method cannot specify any qualifications. And the entire collection will be deleted, including indexes and other information, very useful!

Db.emp.drop ()

Query

The find method is used in MongoDB to make queries. A query returns a subset of documents in a collection, ranging from 0 documents to the entire collection.

The find method accepts two parameters:

The first parameter determines which documents to return, and the content of the parameter is the condition of the query.

The second parameter specifies the desired key (field). The second parameter exists: a key value of 1 means to display, and a value of 0 means no display. "_ id" is displayed by default, and other defaults are not displayed. Where the second parameter does not exist: all fields are displayed by default.

> db.emp.find ({"ename": "sarah"}, {"ename": 1})

{"_ id": 8, "ename": "sarah"}

>

Common operators: $lt, $lte, $gt, $gte, $ne, $in, $nin, $or, $exists, $not, $mod, $size, regular expression

Query criteria:

The $lt, $lte, $gt, and $gte $ne comparison operators (there is no $eq operator) correspond to =,! =, respectively.

> db.emp.find ({"age": {"$lt": 25}}, {"ename": 1}) {"_ id": 2, "ename": "eric"} {"_ id": 5, "ename": "Mark"} {"_ id": 6, "ename": "marry"} {"_ id": 8 "ename": "sarah"} > db.emp.find ({"age": {"$gt": 25}}, {"ename": 1}) {"_ id": 3, "ename": "robin"} {"_ id": 4, "ename": "jack"} {"_ id": 7, "ename": "hellen"} > db.emp.find ({"age": 25}) {"ename": 1}) {"_ id": 1, "ename": "tom"} >

$in, $nin, used to query multiple values of a key.

> db.emp.find ({"age": {"$in": [22pc25 id 28]}, {"ename": 1}) {"_ id": 1, "ename": "tom"} {"_ id": 4, "ename": "jack"} {"_ id": 5, "ename": "Mark"} > db.emp.find ({"age": {"$nin": [22pr 25 28]}}) {"ename": 1}) {"_ id": 2, "ename": "eric"} {"_ id": 3, "ename": "robin"} {"_ id": 6, "ename": "marry"} {"_ id": 7, "ename": "hellen"} {"_ id": 8, "ename": "sarah"} >

$or, which is used to query multiple values of multiple keys (can be used with $in, etc.)

> db.emp.find ({"$or": [{"age": 25}, {"salary": 7800}]}, {"ename": 1) ename: "tom", "age": 25, "salary": 7200} {"ename": "Mark", "age": 22, "salary": 7800} >

$exists, the key corresponding to the query is null, and documents with null and non-existent keys are returned by default. You can determine whether the key exists by $exists.

> db.emp.find ({"language": {"$exists": false}}) {"_ id": 1, "ename": "tom", "age": 25, "department": "Sales", "salary": 7200} {"_ id": 2, "ename": "eric", "age": 24, "department": "HR", "salary": 5400} {"_ id": 3 Ename: "robin", "age": 30, "department": "Sales", "salary": 9600} {"_ id": 4, "ename": "jack", "age": 28, "department": "Development", "salary": 9600} {"_ id": 5, "ename": "Mark", "age": 22, "department": "Development" "salary": 7800} {"_ id": 6, "ename": "marry", "age": 23, "department": "Planning", "salary": 6000} {"_ id": 7, "ename": "hellen", "age": 32, "department": "HR", "salary": 7200} {"_ id": 8, "ename": "sarah" "age": 24, "department": "Development", "salary": 8400} > db.emp.find ({"language": {"$exists": true}) >

$mod, take the mold. For example, find more than 2 records of age field models.

> db.user.find ({"age": {"$mod": [10j2]}}) {"_ id": 6, "ename": "marry", "age": 22, "department": "Planning", "salary": 5000} >

$size, which returns the record of the specified size array element

> db.user.find ({"language": {"$exists": true}}) {"_ id": 4, "ename": "jack", "age": 27, "department": "Development", "salary": 8300, "language": ["Chinese", "English", "Russian", "German" "Japanese"]} > db.user.find ({"language": {"$size": 5}}) {"_ id": 4, "ename": "jack", "age": 27, "department": "Development", "salary": 8300, "language": ["Chinese", "English", "Russian", "German", "Japanese"]}

$all, db.collection.find ({"KeyName": {"$all": [value1,value2]}}) to determine whether value1 and value2 are fully included in KeyName

> db.user.find ({"language": {"$all": ["English", "Latin"]}}) > db.user.find ({"language": {"$in": ["English", "Latin"]}}) {"_ id": 4, "ename": "jack", "age": 27, "department": "Development", "salary": 8300, "language": ["Chinese", "English", "Russian" "German", "Japanese"]}

Regular expression:

> db.user.find ({"ename": / .ark / I}) {"_ id": 5, "ename": "Mark", "age": 21, "department": "Development", "salary": 6500} > db.user.find ({"ename": / .a.k / I}) {"_ id": 4, "ename": "jack", "age": 27, "department": "Development" "salary": 8300, "language": ["Chinese", "English", "Russian", "German", "Japanese"]} {"_ id": 5, "ename": "Mark", "age": 21, "department": "Development", "salary": 6500} >

Where, which allows you to execute arbitrary javascript in a query, so that you can do (almost) anything in the query. For security reasons, the use of the "$where" statement should be strictly restricted or eliminated.

Db.person.find ({"$where": function () {

.; / / this can be any javascript statement.

})

Using cursors, you can limit the number of results, skip part of the results, sort the results according to any combination of keys in any order, or perform other powerful operations.

Var cursor = db.person.find ()

While (cursor.hasNext ()) {

Obj = cursor.next ()

...; / / anything can be done here.

}

Commonly used shell:

Limit: returns only the previous number of results.

Db.person.find (). Limit (2)

Skip: how many results are skipped and then the remaining ones are displayed.

Db.person.find (). Skip (2)

Sort: used for sorting. Accept an object (a set of key-value pairs) as a parameter, the key corresponds to the key name of the document, and the value represents the direction of sorting. The direction of sorting can be 1 (ascending) or-1 (descending). If multiple keys are specified, they are sorted one by one in the order in which they are specified.

Db.person.find (). Sort ({"name": 1, "age":-1}) / / query results, sorted by name ascending order and age descending order

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