MongoDB (2): Delete and change the operation

Source: Internet
Author: User

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 Library

> Use library name;

4. Display all the collections of the library

> Show Collections; or show tables;

5. Display the currently used library

> db;

I. Operating database, Documentation

1.1. Database operation

1. Create a database: MongoDB does not have a specific statement to create a database, you can use "using" to use a database, if you want to use

Database does not exist, you will create one that will be saved as a file after you actually add a document to the library.

> Use db_test;

2, delete the database, command: Db.dropdatabase (), note first to go to the library to be deleted

> Use db_test;switched to DB db_test> db.dropdatabase (); {"OK": 1}>

1.2. Set operation

1. Create a collection: You do not have to create a collection in MongoDB because there is no fixed structure, directly using the DB. Set name. command to operate on it. If you want to display the Create collection, use: Db.createcollecion ("collection name");

2. Delete collection, command: Dorp

> Db.test1.drop ();

1.3. View the status information for a collection

Db. Collection name. Stats ();

Second, increase the deletion

2.1. Add

Db. Collection name. Insert (data);

> Db.test1.insert ({"username": "Zhangsan", age:2});

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/96/C2/wKioL1klLcnTQbcBAAAXd7jDxtg151.jpg "title=" 1495608825 (1). jpg "alt=" wkiol1kllcntqbcbaaaxd7jdxtg151.jpg "/>

Insert method, you can insert a single document, or you can insert multiple, with "[]". Attention:

1:mongodb automatically adds a "_id" field for each document that does not have a "_id" field

2: Each doc must be less than 16MB

3: Object.bsonsize (document name) can be executed in shell, to see size

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/96/C2/wKiom1klL3TRCs5sAAAPRqM2TOA482.jpg "title=" 1495609248 (1). jpg "alt=" wkiom1kll3trcs5saaaprqm2toa482.jpg "/>

2.2. Delete

Command: Remove, can be removed by condition

Just delete the document, 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 a condition, all documents in this collection are deleted.

2.3. Update

Db. Collection name. Update (condition, new document);

> db.test1.update ({"userid": "1"},{"userid": "1", "username": "Zhangsan", "Age": 10});

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/96/C3/wKioL1klM8LwI-LdAAA63JzWYEs248.jpg "title=" 1495610351 (1). jpg "alt=" wkiol1klm8lwi-ldaaa63jzwyes248.jpg "/>

Problem:

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

2, this modification is the entire document

Solve:

Using modifiers


Update modifier for complex update operations

1: $set: Specifies the value of a field, if the field does not exist, creates a

> db.test1.update ({"UserId": "1"},{"$set": {"username": "Zhangsan"}},0,1);

Description: If the UserID is 1, update its username to Zhangsan

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/96/C3/wKioL1klNhmy_w9CAAAnSjPDXCU007.jpg "title=" 1495610952 (1). jpg "alt=" wkiol1klnhmy_w9caaansjpdxcu007.jpg "/>

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

Description: Update the array score with a value of 1 for index 7, and the index to start at 0.

2: $unset: Remove a field

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

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

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

Description: If the UserID is 1, the value of age is increased by 3

4: $push: Add an element to the end of an existing array, or create a new array if not

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

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/96/C8/wKiom1klT1ejcpY3AAAkaC3G-14264.jpg "title=" 1495617408 (1). jpg "alt=" wkiom1klt1ejcpy3aaakac3g-14264.jpg "/>

5: $each: Manipulate multiple values with one $push

> db.test1.update ({"UserId": "1"},{"$push": {"score": {$each: [4,5,6]}}},0,1];

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/96/C9/wKioL1klT8rSYKnpAAAlIS9uDsw894.jpg "title=" 1495617531 (1). jpg "alt=" wkiol1klt8rsyknpaaalis9udsw894.jpg "/>

6: $slice: The limit array contains only the last n elements, whose value must be a negative integer

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

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/96/C8/wKiom1klUG-QmK_4AAAmf27xuAo348.jpg "title=" 1495617694 (1). jpg "alt=" wkiom1klug-qmk_4aaamf27xuao348.jpg "/>

7: $sort: Elements in an array, sorts the data according to the specified fields (1 is ascending,-1 is descending), and then it is deleted by slice.

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

> db.test1.update ({"UserId": "1"},{"$push": {"score": {$each: [], $slice: -5, $sort:-1}});

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/96/C9/wKioL1klUdGyPmLlAAAmagsXkl0637.jpg "title=" 1495618041 (1). jpg "alt=" wkiol1kludgypmllaaamagsxkl0637.jpg "/>

8: $ne: Determine if a value is in an array, and if not, add it in

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

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/96/C9/wKiom1klVQLTN-LFAAANgDyggNk762.png "title=" 72770efc-1522-4214-9c74-490be985c533.png "alt=" Wkiom1klvqltn-lfaaangdyggnk762.png "/>

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

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

: $pop: Delete the element from the end of the array, {$pop: {key:1}}, delete one from the end, 1 remove from the head

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

All: $pull: Delete all matching elements by condition

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

12:$: Used to modify the first matching element

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

Description: If the No. 0 index value of score is 5, the No. 0 index value of update score is 7

























































This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1929096

MongoDB (2): Delete and change the operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.