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

Add, delete and modify operations in MongoDB

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

Share

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

Addition, deletion, modification and search of MONGODB:

Note: the field "_ id" is the GUID added to us by default by the database to ensure the uniqueness of the data.

1. Add insert

1. Grammar: db. Collection .insert ({"key1": value1, "key2": value2,. "keyN": valueN})

> db.wqq.insert ({"name": "wqq", age:25,height:1.65,dep: "DEGREE"})

WriteResult ({"nInserted": 1})

# # insert a value that can specify _ id, as well as nesting between other values

For (iSuppli id: 119, "age": 41, "name": "aaa19"})

WriteResult ({"nMatched": 0, "nUpserted": 1, "nModified": 0, "_ id": 119})

2. Insert multiple entries:

Db.abc.insert ([{name: "wangwu", age:22,sex:1,class: "jsj"}, {name: "aaa", age:22,sex:1,class: "bdd"}])

2. Find find

1. Find all: db. Collection. Find ()

2. Conditional search: db. Collection. Find ({"key1": value1, "key2": value2,. "keyN": valueN})

3. Other statistics

Db.wqq.count () db.wqq.find () .count () / Statistics

Db.wqq.find (). Sort ({name:-1}) / / sort-1 is descending and 1 is ascending

Db.wqq.find (). Limit (4) / / how many

Db.wqq.find (). Limit (4). Pretty () / / formatted display

Db.wqq.find () .skip (4) .limit (10) / / Skip several values

Db.wqq.find (). Skip (4) .limit (10). Sort ({"_ id":-1}) / / take several values by sorting them

Db.wqq.find ({"age": {$all: [110]}})

Db.wqq.find ({"age": {$in: [110111112.]}}) / / returns the value that exists in the array

Db.wqq.find ({"age": {$nin: [110111112.]}}) / / returns the value that exists in the array

Db.wqq.find ({"age": {$exists:1}}) / / check whether a field exists

Db.wqq.find ({"age": {$mod: [10LJ 2]}}) / / returns the value equal to the remainder after the field is modeled.

Db.wqq.find ({$or: [{"age": 40}, {"name": "aaa19"}]}) / / View the records of specified conditions, similar to SQL's OR

Db.wqq.find ({$nor: [{"age": 40}, {"name": "aaa19"}]}) / / contrary to or

# # comparison operator

$gt: greater than $gte: greater than or equal to $lt: less than $lte: less than or equal to $ne: not equal to

Db.wqq.find ({"age": {$lt:100}})

# # logical Operation

And operation:

Db.wqq.find ({age:23,name: "aaa1"})

Or ($in) / not $nin

Db.wqq.find ({age: {$in: [11Jing 222pm 33444]}})

# # regular matching

Db.wqq.find ({name:/aaa/})

# # $where

/ / return the document containing the login_name field

Db.getCollection ('users') .find ({$where:function () {

Return!! this.login_name

})

# # and Operation

Db.wqq.find ({name: "wangwu", class: "jsj"})

# # combined query

Db.abc.find ({name: {$exists:1,$nin: ["Zhang San"]}}) / / field exists and is not called "Zhang San"

3. Update update ()

1. Update a field:

Db.wqq.update ({age:42}, {$set: {name: "sbt"}})

2. Add a field value to a document or if it does not exist

Db.wqq.update ({name: "aaa78"}, {$inc: {age:2}}, 0jin1)

3. Batch update. If there is an update in the field, if there is no additional field in the field.

Db.wqq.update ({name: "aaa"}, {$set: {age:2}}, false,true)

Db.wqq.update ({name: "Zhang San"}, {$set: {age:3}}, 0jin1)

4. A collection has only one key-value update

Db.wqq.update ({name: "Zhang San"}, {name: "Mo Wing Lun"})

4. Delete

1. Delete some documents

Db.wqq.remove ({"name": "abc"})

2. Delete the entire collection:

Db.wqq.drop ()

3. Delete which article

Db.wqq.remove ({name: "abc"}, 2)

4. Delete a field

Db.abc.update ({}, {$unset: {age:50}}, 0jue 1) / / Delete multiple fields, and delete the first field without adding 0J1.

5. The use of cursors

Var x=db.wqq.find ()

X.next ()

X.hasNext ()

While (x.hasNext () = = true) {x.next ()}

6. Add an array to the collection and pop up

Db.abc.update ({}, {$push: {arr:3}}) / / add a worthy array

Db.abc.update ({}, {$pushAll: {arr: [1, 2, 3, 4, 5, 6, 7, 8, 9]}) / / add multiple worthy arrays

Db.abc.update ({}, {$pop: {arr:1}}) / / the last value of the pop-up array, and-1 is the first value of the array

Db.abc.update ({name: "Zhang San"}, {$pull: {arr:4}}, 0jin1) / / deletes the specified value of the array

Db.abc.update ({}, {$addToSet: {arr:6}}, 0J1) / / add an array to the array, and do not repeat it if it exists.

Db.abc.update ({name: "Zhang San"}, {$addToSet: {$each: {$each: [1meme 2meme 3meme 4]}, 0meme 1) / / is used to run the modifier to manipulate data to the collection multiple times.

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