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 (2): add, delete and modify operation

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

Share

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

Additional commands:

1. Enter the front-end operation command

. / mongo [ip: Port]

Description: local is automatically selected by default, port 27017

2. Show all libraries

> show dbs; or show databases

3. Select the library

> use library name

4. Display all collections of the library

> show collections; or show tables

5. Display the libraries currently in use

> db

First, operate databases and documents

1.1. Database operation

1. Create a database: MongoDB does not have a statement specifically to create a database. You can use "use" to use a database. If you want to use the

If the database does not exist, it will be created and saved as a file after actually adding a document to the library.

> use db_test

2. Delete the database. Command: db.dropDatabase (). Please enter the library to be deleted first.

> use db_test;switched to db db_test > db.dropDatabase (); {"ok": 1} >

1.2, collection operation

1. Create a collection: there is no need to create a collection in MongoDB, because there is no fixed structure, you can directly use db. Name of the collection. Command to operate on it. If you do not want to show create a collection, use: db.createCollecion ("collection name")

2. Delete the collection, command: dorp

> db.test1.drop ()

1.3. View the status information of the collection

Db. Collection name .stats ()

II. Adding, deleting and correcting

2.1, add

Db. Collection name .insert (data)

> db.test1.insert ({"username": "zhangsan", age:2})

The insert method, which can insert a single document or multiple documents, can be used with "[]". Note:

1:MongoDB automatically adds a "_ id" field to each document that does not have a "_ id" field

2: each Doc must be less than 16MB

3: you can execute Object.bsonsize (document name) in shell to view the size size

2.2, delete

Command: remove, which can be deleted according to conditions

Just delete the document, and the collection is still there. If you use the drop command, the collection and index will be deleted.

> db.test1.remove ({age:2})

Note: if you use remove without conditions, all documents in this collection will be deleted.

2.3. Update

Db. Name of the collection. Update (condition, new document [, boolean,boolean])

> db.test1.update ({"userId": "1"}, {"userId": "1", "username": "zhangsan", "age": 10})

Question:

1. If there are multiple documents matching here, only the first document will be updated.

2. This modifies the whole document

Resolve:

Use modifier

Update modifier, used to do complex update operations

1:$set: specifies the value of a field. If the field does not exist, a field is created.

> db.test1.update ({"userId": "1"}, {"$set": {"username": "zhangsan"}}, 0Magne1)

Note: if userId is 1, update its username to zhangsan

> db.test1.update ({"userId": "1"}, {$set: {"score.1": 7}})

Description: update the array score with an index of 1 with a value of 7, starting with 0.

2:$unset: delete a field

> db.test1.update ({"userId": "1"}, {"$unset": {"username": 1}}, 0Jing 1)

3:$inc: used to increase the value of an existing key, and if a field does not exist, one will be created. Can only be used for integer, long integer, or double-precision floating-point values.

> db.test1.update ({"userId": "1"}, {"$inc": {"age": 3}}, 0Jing 1)

Note: if userId is 1, increase its age value by 3

4:$push: add an element to the end of an existing array. If not, create a new array.

> db.test1.update ({"userId": "1"}, {"$push": {"score": 1}}, 0Jing 1)

5:$each: manipulate multiple values through $push at a time

> db.test1.update ({"userId": "1"}, {"$push": {"score": {$each: [4pm 5je 6]}, 0mem1)

6:$slice: restricts the array to contain only the last n elements, whose values must be negative integers

> db.test1.update ({"userId": "1"}, {"$push": {"score": {$each: [7, 8, 10, 9], $slice:-5}})

7:$sort: sorts the data by the specified fields (1 for ascending order,-1 for descending order) for the elements in the array, and then deletes them by slice.

Note: you cannot just use $slice or $sort with $push, and you must use $each

> db.test1.update ({"userId": "1"}, {"$push": {"score": {$each: [1Jing 2Jing 3], $slice:-5,$sort:-1})

8:$ne: determines whether a value is in the array, and if not, adds it

> db.test1.update ({"userId": "1", "score": {$ne:4}}, {$push: {"score": 4}})

9:$addToSet: use the array as a dataset to ensure that the elements in the array are not duplicated

> db.test1.update ({"userId": "1"}, {$addToSet: {"score": 8}})

10:$pop: delete the element from one end of the array, {$pop: {key:1}}, delete one from the end, and-1 delete from the header

> db.test1.update ({"userId": "1"}, {$pop: {score:1}})

11:$pull: delete all matching elements according to the condition

> db.test1.update ({"userId": "1"}, {$pull: {score:7}})

12virtual: used to modify the first matching element

> db.test1.update ({"score.0": 5}, {$set: {"score.$": 7}})

Note: if the 0 th index value of score is 5, the 0 th index value of update score is 7

III. Supplementary methods

3.1.The save method

Update if the document exists, and create a new one if it doesn't exist, mainly based on "_ id".

Add:

Update:

3.2.The upsert method

Update the document that meets the criteria, otherwise a new document will be created with this condition and the updated document.

Specifies that the third parameter of the update method is true, which can be expressed as upsert

3.3. Update multiple documents

By default, MongoDB only updates the first document that meets the criteria. To update all matching documents, put the fourth parameter

Set to true. Note:

1: can only be used in the operation of $XXX

2: it is best to display the fourth parameter of the specified update every time to prevent the server from using the default behavior

3.4. Query how many documents have been updated

Use the command: getLastError to return the information about the last operation, where n is the updated text

The number of files. Such as: db.runCommand ({"getLastError": 1})

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