Mongdb_crud_ Simple that example explained

Source: Internet
Author: User

First, the data operation Example explains1, data query

NOSql:db.user.find ({age:{$gt: 20}},{name,age}). Limit (5); Query the user name and age of the first five (limit (5)) records in the user collection that are greater than age>20.

Split:

1. Verify that the database data collection performs the Operation Db.user.find

2, criteria of criteria is thecondition {age:{$gt: 20}},{name,age}; We can understand that we need to pass in parameters to execute the Find function.

there are two conditions for a specific parameter age:{$GT: a result {name,age} }

3, aggregate function: After I have found the data on the basis of this data to filter;. Limit (5);

Sql:select a.name,a.age from User a WHERE a.age>20 LIMIT (0,5);


2, Data insertion

NOSql:db.user.insert ({name: "ZG", age:25,sex:1});

Split:

1. Confirm that the database data collection performs the operation

2,data data: The data that executes the operation; The data that we insert into the database

Sql:inserte into User (Name,age,sex) VALUES ("ZG", 25, 1);

Note: When we perform an insert operation without the _ID primary key field, MGDB automatically inserts a unique field as the primary key


3, Data deletion

NOSql:db.user.remove ({sex:1});

Split:

1. Confirm that the database data collection performs the operation

2,criteria Standard condition {sex:1}

Sql:delete user WHERE sex = 1;

Note: Delete the operating room Delete all data documents that meet the criteria; if we want to delete only one document record that satisfies the criteria, challenge set option


4, Data Update:

NOSql:db.user.update ({age:{$gt: 18}},{$set: {sex:2}},{multi:true});

Split:

1. Confirm that the database data collection performs the operation

2, Criteria criterion is thecondition {age:{$gt:}}

3,action operation is what we want to update {$set: {sex:2}}

4,option Options

Sql:update user SET sex = 2 WHERE age > 18;

Note: 1,MOGDB will default to update the only one qualifying document, we need to specify the Mutli option when we want to update multiple documents that meet the criteria;

2, if we need to be with the new data does not exist we can set option:{upsert:true} to add a record;




Mongdb_crud_ Simple that example explained

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.