Create a document
Inserting data into MongoDB, using INSERT,
such as: Db.refactor.insert ({"Refactor ' s Blog": "Http://www.cnblogs.com/refactor"})
This action adds a "_id" to the document, and the key is saved in the database.
BULK INSERT
If you want to insert multiple documents, using BULK Insert will be faster, BULK insert can pass in a document composed of data to the database
A BULK insert is just a single TCP request, avoiding the overhead of many fragmented requests. Because there is no need to process a large number of message headers,
This reduces the insertion time. Whenever a single document is sent to a database, there is a header message that tells the database to do the specified collection
Insert operation. With bulk insertion, the database does not have to process this header information for each document repeatedly.
The maximum length of the current version of MONGODB messages is 16M, which is limited by the use of bulk inserts.
Principle and function of insertion
When an insert is performed, the driver used converts the data into a Bson form and then feeds it into the database. Database parsing
BSON, the test contains "_id" key and not more than 16M, in addition, do not do other verification, it is simply the document as it is
into the database. The disadvantage of this is that it allows you to insert invalid data, and the advantage is that it makes the database more secure and far away from injection attacks.
The driver of all major languages will check the validity of some data before transmitting the data (whether the document is very long, whether it contains non-utf-8 characters,
Whether an unknown type is used). You can also start the database server by using the--objcheck option so that the server inserts
Verify the validity of the document structure before sacrificing performance.
View the size (in bytes) of the doc document converted to Bson, using Object.bsonsize (DOC) in the shell
Delete a document
Db.users.remove ()
All documents for the Users collection are deleted, but the collection itself is not deleted, and the original index is retained.
The Remove function can accept a query document as an optional parameter, given this parameter, only the document that meets the criteria will be deleted.
Db.users.remove ({"Name": "Refactor"})
Deletion of data is permanent and cannot be undone and restored.
If you want to clear the entire collection, deleting the collection directly (and then rebuilding the index) is faster than deleting all the documents in the collection.
Delete collection: Db.drop_collection ("Users")
Update document
Update has two parameters, one for querying the document, for finding the document to be updated, and the other is a modifier document that describes the changes to the found document
The updated operation is atomic, and if two updates occur at the same time, first to the server and then to the other. The last update is displayed in the database
Document replacement
The simplest thing to update is to use a new document instead of a matching document, which is suitable for a time when the pattern structure has changed significantly. If the following document
{
"Name": "Refactor",
"Friends": 20,
Enemies:2
}
Change to
{
"Name": "Refactor",
"Relationships":
{
"Friends": 20,
Enemies:2
}
}
Using modifiers
Usually the document will only be part of the update, using the atomic "update modifier", update modifier is a special key to specify complex update operations, such as:
Adjust, add, delete keys, manipulate arrays, manipulate inline documents.
General Web sites have counters to count the number of public access, you can use atomic modifier atom to complete the function.
Getting started with the $set modifier
The "$set" is used to specify the value of a key, which is created if the key does not exist, which is convenient for updating the mode or adding user-defined keys.
such as:
Db.users.insert (
{
"name": "Refactor",
"age": All,
"sex": "Male"
}
)
Add favorite books:
db.users.update (
{
"name": "Refactor"
},
{
"$set": {"book": "War and Peace"}
}
)
db. Users.findone (
{"name": "Refactor"}
)
Modify your favorite data
Db.users.update (
{
"name": "Refactor"
},
{
$set: {"book": "War and Peace2"}
}
)
"$set" can modify the data type of the key, and if you prefer multiple books,
change the value of the book key to an array
Db.users.update (
{
"name": "Refactor"
},
{
"$set": {"book": ["War and Peace", "War and Peace2"]}< br>}
)
use "$unset" to delete the key, no key to delete or error
Db.users.update (
{
"name": "Refactor"
},
{
"$unse T ": {" book ": 1}
}
)
use" $set "to modify the inline document
Db.blog.insert (
{
" title ":" Refactor's blog ",
" Content ":" Refactor ' s blog test ',
' author ':
{
' name ': ' Refactor ', '
' email ': ' [email protected] '
}
}
)
Db.blog.update (
{
"Author.name": "Refactor"
},
{
"$set": {"Author.name": "Refactor2"}
}
)
Db.blog.findOne (
{
"title": "Refactor ' s Blog"
}
)
Add, modify, and delete keys when you should use the $ modifier. Be sure to use the modifier that starts with $ to modify the key-value pair.
Use the $inc to increase and decrease
The "$inc" is used to increase the value of the existing key, or increase if no key exists. Used to analyze data, causality, polls or other places where there is a change in value.
Such as:
Db.users.insert (
{"url": "Http://www.cnblogs.com/refactor"}
)
Use "$inc" to add a key pageviews, the default value is 10000
Db.users.update (
{"url": "Http://www.cnblogs.com/refactor"},
{"$inc": {"pageviews": 10000}}
)
Use "$inc" to pageviews the key, then add 10000.
Db.users.update (
{"url": "Http://www.cnblogs.com/refactor"},
{"$inc": {"pageviews": 10000}}
)
Use "$inc" to pageviews the key and reduce it by 10000.
Db.users.update (
{"url": "Http://www.cnblogs.com/refactor"},
{"$inc": {"pageviews":-10000}}
)
The "$inc" and "$set" usages are similar in that they are used to increase or decrease numbers. " $inc "can be used only for integers, long integers, or double-precision floating-point numbers.
While many languages can convert null,bool, digitally formed strings into numbers, using "$inc" will still cause errors:
The value of the $inc key must also be a number.
Array modifiers
"$push" and "$pop" can only be used for arrays.
If the specified key already exists, "$push" adds an element to the end of an existing array and creates a new array if the key does not exist.
Such as:
Db.blog.insert (
{
"title": "Refactor ' s Blog",
"Content": "Refactor ' s blog test",
"Author":
{
"Name": "Refactor",
"Email": "[email protected]"
}
}
)
To the above document, add a "comment" key that contains an array, and push a comment to the comment array.
This array is automatically created and added to the comments:
Db.blog.update (
{"title": "Refactor ' s Blog"},
{
$push:
{
"Comments":
{
"Name": "Refactor2",
"Content": "Nice"
}
}
}
)
Also want to add a comment:
Db.blog.update (
{"title": "Refactor ' s Blog"},
{
$push:
{
"Comments":
{
"Name": "Refactor3",
"Content": "Good"
}
}
}
)
If a value is not in the array, add it in. You can use $ne when querying a document.
Use "$addToSet"
Db.users.insert (
{
"UserName": "Refactor",
"Emails":
[
"[Email protected]",
"[Email protected]"
]
}
)
Add new addresses to emails and avoid duplicates with "$addToSet"
Db.users.update (
{"UserName": "Refactor"},
{
"$addToSet":
{
"Emails": "[email protected]"
}
}
)
This "[email protected]" will not be inserted into the emails
Db.users.update (
{"UserName": "Refactor"},
{
"$addToSet":
{
"Emails": "[email protected]"
}
}
)
This "[email protected]" will be inserted into the emails
You can add different values by using $addToSet and $each.
Db.users.update (
{"UserName": "Refactor"},
{
"$addToSet":
{
"Emails":
{
"$each":
[
"[Email protected]",
"[Email protected]",
"[Email protected]"
]
}
}
}
)
With $pop, you can delete an element from either end of the array.
{$pop: {key:1}} removes an element from the end of the array
{$pop: {key:-1}} removes an element from the head of the array
With $pull, you can delete elements based on specific criteria, or you can delete elements based on location
Db.users.update (
{"UserName": "Refactor"},
{
"$pull":
{
"Emails": "[email protected]"
}
}
)
"$pull" will delete all the matching parts, and for the array [1,1,2,1] to execute pull 1, the resulting array is [2]
MONGODB Create update syntax