Summary
The previous article learned about MongoDB installation on Windows, and how to open MongoDB, and finally lists simple additions and deletions to check the operation. This article will continue in-depth study and add and delete changes.
Related articles
Getting started with [MongoDB]
Crud
In the previous article, we know that each object stored in collection is a Document object. Document is a JSON-form object that exists as a key-value pair. We all know that the data stored in the value of JSON can be a single object, a string, an array, or possibly a nested JSON type of data.
Insert
Insert operations are also divided into: single insert and BULK INSERT.
Case: Create a user that includes the name of the person, the company in which the company contains the company name and address information.
Through the insert operation above, we find that the user includes a nested company JSON object.
Later, because the company app needs to add the Address Book feature, you need to save the user's friends. This is the user's friends can be a data to save the name
About the Bulk insert operation, looked for a long time did not find the right way, it is estimated that MongoDB javasrcipt shell does not support the bulk of the Operation Bar. But you can use C # driver, a disguised implementation of bulk INSERT, a loop one by one insert.
Find
Find operations, find support for >,>=,<,<=,!=,=, these comparators in MongoDB correspond to $GT, $gte, $lt, $lte, $ne.
Insert test data to do a find operation
Find users with age equal to 20
Find users older than or equal to 18
Other comparators no longer have an example, mimicking $gte operations.
$exists If a field exists
Find the document that has the company field
Find the user who does not have an address field
$in: In a range
Find the company address in the Beijing,shanghai user
$nin: Not in a range
Db.users.find ({"company.address": {$nin: ["Beijing", "Shanghai"]}})
$or: Or
Find users in Beijing, or users over 20 years of age
Regular expression Query
Complex condition query can be implemented by $where, JS function can be used as the value of $where, using JS to implement complex condition query.
Update
MongoDB update, divided into the whole update and the partial update.
Overall update: It is all the change does not change the need to update, a bit like an update in EF, first query out the object, and then modify the value of a field, and then update back.
Local update: Just update the required fields.
MongoDB provides us with two keywords: $inc, $set used to implement the update operation for MongoDB.
$inc: Increment, is the abbreviation of increase. Add a value on the original basis.
Modify the age of the user aged 29, on this basis add-9
$set
Usage is similar to set in SQL
The Update method is the third parameter, the bool type parameter, which defaults to False, and if true, identifies the action created if it does not exist.
Update the user information named Zhangsan, which does not exist then creates
Batch Update
Set the fourth parameter to True
Remove (where)
Summarize
This article has learned about MongoDB additions and deletions to the operation. Do it once and deepen your impressions.
Reference
Http://www.cnblogs.com/huangxincheng/archive/2012/02/19/2357846.html
[MongoDB] additions and deletions change