Mongodb4--mongodb and additions and deletions change

Source: Internet
Author: User

MongoDB basic operation of the increase and deletion check

We know that the most common operation of a traditional relational database is "Add/remove/Modify/query", i.e.
The legendary crud (Create/remove/updte/delete).

In learning MongoDB, we also start with crud operations.

Let's start by creating a library with the name test:


Create a student table:


1. New operations
(1) Add a single document (db. Collectionname.insert ({JSON data}))
We insert a piece of data for the school table:

MONGO will define a _id primary key for each row of data, and you can specify it manually if you want to change the primary key:


(2) Add multiple data at once (using [{},{},{} ...])
JSON is an object that has the concept of an array,
You only need to put multiple formations into an array to insert, you can implement bulk Add.

Note: Arrays in JSON are made up of "[]" brackets.

2. Delete operation
(1) Delete a single piece of data in the table (db. Collectionname.reomve ({delete condition, option}))
We delete the data in the student table with the code attribute value ' 004 ':


Attention:
1. The query expression is still a JSON object
2. Rows that match the query expression will be deleted
3. If you do not write a query expression, all documents in the collection will be erased

We insert a few more messages:

Inserted two men's information and two women's information.

We delete all the ladies and can make sex in the deletion condition: ' Women ' can be deleted:


(2) Delete option
We can add an option after the delete condition, meaning (justone:true/false), whether
Deletes only one row. The default is False.

Below we delete two men, add a "true" in the back, found that only one row to meet the criteria
The information:



3. Modifications
Change who? --Query expression
What's the change? --New value or assignment expression
Action Options--optional parameters

(1) Modify the information in the document (db. Collectionname.update ({old parameter},{new parameter}))
We changed the name of Tom to "Tomsmith."


The name attribute was actually modified, but we should note that Tom's other attributes of the document
It's all gone!

Actually very good explanation, "{}" curly braces in the general generation of an entire document, so when we develop
When a parameter corresponds to a document's new information, the entire document is modified.

What if we want to modify one of the parameters and don't want to change the other data in the document? We
You can use the "set" assignment expression to modify it.

(2) Modify one of the properties of the document (db. Collectionname.update ({old parameter},{$Set: {new Parameter}}))
We changed the name of Jack to "Jackson."


We can see that only the name has been modified and the other properties have not been affected.

(3) Assignment expression when modified
$set Modify the value of a column
$unset Delete a column
$rename Rename a column
$inc Grow a column

Let's give an example to illustrate these parameters.

Example 1:
First we create a document information, which is a person's information.
This is a female student, there is no bank card, currently only 100 yuan money.

After graduating from college, the schoolgirl became a businessman and we were going to take her role information
Change to "merchant (businessman)", use $set to modify; Then she had money,
To remove her "no bank card" This attribute, using $unset;
Girls earn 10 billion, to add to the previous money above, the use of $INCR;
Girls dislike sex attribute not good, spend money changed to "gender (gender)", use $rename.


(4) Modify options
{Upsert:true/false,multi:true/false}
Upsert refers to a row that does not have a match, insert the row directly (as with replace in MySQL)
Multi is to modify multiple rows (the default is False, one row is modified).

Cases:

There is no document for _id 99, so insert the document directly

Cases:

Db.student.update ({Gender: ' Man '},{gender: ' Women '},{multi:true});
Change all the gender for "man" to "women".

Say the above two options, add an assignment expression "$setOnInsert", meaning
When Upsert is true, the insert operation occurs because there is no document
You can fill in some fields.

Let's say we update the Code 066 document (not in the database, the insert operation will occur), we can
Use $setoninsert to add attributes when an insert occurs:



4. Enquiry
Syntax: DB. Collectionname.find (query expression, query column);
Db. Collectionname.find (expression, {column 1:1, column 2:1, column 3:0});
That is, the property you want to display is followed by the number 1, and the following number 0 is not displayed.

(1) Querying all documents
Db. Collectionname.find ();

(2) only one attribute (db) is queried. Collectionname.find ({},{property name}))
Query for information about people with gender in all documents:


Query information for people with gender in all documents and do not query the id attribute:


The above is MongoDB's additions and deletions to change all the operations.

In fact, the database crud operations are inseparable from a thing, that is, "query expression", that is, where/or/and/not and other statements, which are summarized in the next chapter to you.

Mongodb4--mongodb and additions and deletions change

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.