MongoDB Study Notes (Data Operations)

Source: Internet
Author: User
Tags findone

1. Batch insert:
Inserting multiple documents at a time as an array can be completed in a single TCP request, avoiding additional overhead in multiple requests. In terms of data transmission volume, batch insert data only contains one message header, while multiple single inserts encapsulate the message header data each time the data is inserted. For data import, we can use volume import.
 
2. Clear the database:
> Db. users. remove ()
The preceding command clears all data in the users set, but does not delete the set itself and associated indexes. The data deletion operation is unrecoverable. Once deleted, it is physically deleted. A more effective way for the case of full set clearing is to directly Delete the set object itself and all its associated indexes, and then re-create them in sequence, for example:
> Db. one_collection.drop ()
 
3. Data Update:
If multiple documents match the update conditions when updating data, MongoDB updates only the first query result to avoid repeated conflict of updated _ IDs, for example:
> Post1 = {"name": "stephen", "age": "35 "}
{"Name": "stephen", "age": "35 "}
> Post2 = {"name": "stephen", "age": 36}
{"Name": "stephen", "age": 36}
> Db. blog. insert (post1)
> Db. blog. insert (post2)
> Post3 = {"name": "stephen", "age": 37}
{"Name": "stephen", "age": 37}
> Db. blog. update ({"name": "stephen"}, post3)
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd7e2e20668578cc1097d8"), "name": "stephen", "age": 36}
{"_ Id": ObjectId ("4fcd7e2820668578cc1097d7"), "name": "stephen", "age": 37}

 

4. modifier:
Using modifiers to update data is atomic and efficient. Different from updating all documents, the updated document's _ id does not change, if the document is completely updated, the _ id and related indexes of the document will be modified.
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd7e2820668578cc1097d7"), "name": "stephen", "age": 41}
{"_ Id": ObjectId ("4fcd81bb20668578cc1097d9"), "name": "stephen", "age": 38}
-- $ Inc modifier adds the age key of the document that matches the condition to one. By default, only the first qualified document is updated.
> Db. blog. update ({"name": "stephen" },{ "$ inc": {"age": 1 }})
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd7e2820668578cc1097d7"), "name": "stephen", "age": 42}
{"_ Id": ObjectId ("4fcd81bb20668578cc1097d9"), "name": "stephen", "age": 38}
-- You can use the last parameter of the update function to update all documents that meet the conditions, for example:
> Db. blog. update ({"name": "stephen" },{ "$ inc": {"age": 1 }}, true, true)
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd7e2820668578cc1097d7"), "name": "stephen", "age": 43}
{"_ Id": ObjectId ("4fcd81bb20668578cc1097d9"), "name": "stephen", "age": 39}

-- $ Set modifier directly modifies the content of the matching document. If the modified key exists, it is directly modified; otherwise, it is added.
> Db. blog. update ({"name": "stephen" },{ "$ set": {"genda": "male "}})
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd88b720668578cc1097da"), "age": "35", "genda": "male", "name": "stephen "}
-- $ Unset: the function of modifying $ set is the opposite, for example:
> Db. blog. update ({"name": "stephen" },{ "$ unset": {"genda": "male "}})
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd88b720668578cc1097da"), "age": "35", "name": "stephen "}
-- You can use the $ set modifier to modify nested sub-documents.
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd8e0220668578cc1097db"), "title": "A Blog Post", "author": {"name": "joe", "email ": "joe@ee.com "}}
> Db. blog. update ({"title": "A Blog Post" },{ "$ set": {"author. name": "joe schmoe "}})
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd8e0220668578cc1097db"), "author": {"email": "joe@ee.com", "name": "joe schmoe"}, "title ": "A Blog Post "}

 

5. array modifier:
> Db. blog. insert ({"title": "one blog "})
> Db. blog. find ()
{"_ Id": ObjectId ("4fcd909520668578cc1097dc"), "title": "one blog "}
-- If the operation key does not exist, a new key value is created. The value type is array.
> Log. update ({"title": "one blog" },{ "$ push": {"comments": {"content": "hello "}}})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fcd909520668578cc1097dc "),
"Comments ":[
{
"Content": "hello"
}
],
"Title": "one blog"
}
-- If the key value of the $ push operation already exists and its value is of the array type, the modifier adds a new array element to the array.
> Db. blog. update ({"title": "one blog" },{ "$ push": {"comments": {"content": "word "}}
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fcd909520668578cc1097dc "),
"Comments ":[
{
"Content": "hello"
},
{
"Content": "word"
}
],
"Title": "one blog"
}
 
> Post = {"username": "joe", "emails": ["joe@example.com", "joe@gmail.com", "joe@yahoo.com"]}
{
"Username": "joe ",
"Emails ":[
"Joe@example.com ",
"Joe@gmail.com ",
Joe@yahoo.com"
]
}
> Db. blog. insert (post)
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Username": "joe ",
"Emails ":[
"Joe@example.com ",
"Joe@gmail.com ",
Joe@yahoo.com"
]
}
-- $ AddToSet applies to arrays. If the element already exists in the array, this command will return the result without any operation. Otherwise, the new element will be inserted into the array.
> Db. blog. update ({"username": "joe" },{ "$ addToSet": {"emails": "joe@gmail.com "}})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Username": "joe ",
"Emails ":[
"Joe@example.com ",
"Joe@gmail.com ",
Joe@yahoo.com"
]
}
> Db. blog. update ({"username": "joe" },{ "$ addToSet": {"emails": "joe@hotmail.com "}
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@example.com ",
"Joe@gmail.com ",
"Joe@yahoo.com ",
Joe@hotmail.com"
],
"Username": "joe"
}
-- $ AddToSet and $ each can be used to insert an array into another array.
> Db. blog. update ({"username": "joe" },{ "$ addToSet": {"emails": {"$ each": ["joe@php.net ", "joe@example.com"] }})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@example.com ",
"Joe@gmail.com ",
"Joe@yahoo.com ",
"Joe@hotmail.com ",
Joe@php.net"
],
"Username": "joe"
}
-- $ Pop deletes an element from the array. If the parameter is 1, an element is deleted from the end of the array. If the parameter is-1, the element is deleted from the header.
> Db. blog. update ({"username": "joe" },{ "$ pop": {"emails": 1 }})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@example.com ",
"Joe@gmail.com ",
"Joe@yahoo.com ",
Joe@hotmail.com"
],
"Username": "joe"
}
> Db. blog. update ({"username": "joe" },{ "$ pop": {"emails":-1 }})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@gmail.com ",
"Joe@yahoo.com ",
Joe@hotmail.com"
],
"Username": "joe"
}
-- $ Pull modifier is used to delete the specified element from the data.
> Db. blog. update ({"username": "joe" },{ "$ pull": {"emails": "joe@yahoo.com "}})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@gmail.com ",
Joe@hotmail.com"
],
"Username": "joe"
}
-- Duplicate elements appear in the array to facilitate the demonstration of the modifier.
> Db. blog. update ({"username": "joe" },{ "$ push": {"emails": "joe@gmail.com "}})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@gmail.com ",
"Joe@hotmail.com ",
Joe@gmail.com"
],
"Username": "joe"
}
-- In the array, the subscript of the first element is 0, and then increases sequentially. The following example is to mark the bottom of the array as 1
-- Change the element value of (second element) to a new value.
> Db. blog. update ({"username": "joe" },{ "$ set": {"emails.1": "joe@example.com "}})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@gmail.com ",
"Joe@example.com ",
Joe@gmail.com"
],
"Username": "joe"
}
-- Sometimes, especially when modifying the query results, we cannot know the results document array subscript, MongoDB
-- Provides a subscript for the $ operator to indicate the query result. However, only the first matching element is updated.
> Db. blog. update ({"emails": "joe@gmail.com" },{ "$ set": {"emails. $": "joe@hotmail.com "}})
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2e468b2ac404941134bed "),
"Emails ":[
"Joe@hotmail.com ",
"Joe@example.com ",
Joe@gmail.com"
],
"Username": "joe"
}
 
6. upsert:
Upsert is a special update. If no document meets the update condition, a new document is created based on this condition and the updated document. If a matching document is found, the file is updated normally.
> Db. blog. remove ()
> Db. blog. update ({"username": "joe" },{ "username": "joe", "age": 30}, true)
> Db. blog. findOne ()
{
"_ Id": ObjectId ("4fd2faac576cd9c101ac0f3d "),
"Username": "joe ",
"Age": 30
}
The following example shows how to modify the new value while adding a new one.
> Db. blog. remove ()
> Db. blog. update ({"count": 25 },{ "$ inc": {"count": 3 }}, true)
> Db. blog. find ()
{"_ Id": ObjectId ("4fd2fd59576cd9c101ac0f3e"), "count": 28}
Save is a shell function that can be inserted when the document does not exist and updated when the document exists. Upsert can also do the same job, but it is not as convenient as the save command.
> Var x = db. blog. findOne ()
> X. count = 40
40
> Db. blog. save (x)
> Db. blog. findOne ()
{"_ Id": ObjectId ("4fd2fde4576cd9c101ac0f3f"), "count": 40}
 
7. Return updated documents:
You can use the getLastError command to obtain the number of updated documents when multiple documents are updated.
> Db. blog. remove ()
> Db. blog. insert ({"name": "stephen "})
> Db. blog. insert ({"name": "Stephen 3 "})
> Db. blog. insert ({"name": "Stephen 4 "})
> Db. blog. update ({},{ "$ set": {"name": "liu" }}, false, true)
-- N: 3 indicates that the number of modifications is 3.
> Db. runCommand ({getLastError: 1 })
{
"UpdatedExisting": true,
"N": 3,
"ConnectionId": 1,
"Err": null,
"OK": 1
}
FindAndModify can modify query results atomically or atomically.
> Db. blog. insert ({"name": "stephen "})
> Db. blog. insert ({"name": "Stephen 2 "})
> Db. blog. find ()
{"_ Id": ObjectId ("4fd30cd7f6dccb7c058244"), "name": "stephen "}
{"_ Id": ObjectId ("4fd30cd1_f6dccb7c058245"), "name": "Stephen 2 "}
> Db. runCommand ({"findAndModify": "blog", "query": {"name": "Stephen 2"}, "update": {"$ set": {"name ": "Stephen 3 "}}})
> Db. blog. find ()
{"_ Id": ObjectId ("4fd30cd7f6dccb7c058244"), "name": "stephen "}
{"_ Id": ObjectId ("4fd30cd1_f6dccb7c058245"), "name": "Stephen 3 "}
> RunCommand ({"findAndModify": "blog", "query": {"name": "Stephen 3"}, "remove": true })
> Db. blog. find ()
{"_ Id": ObjectId ("4fd30cd7f6dccb7c058244"), "name": "stephen "}
The findAndModify command has the following values for each key:
FindAndModify: The collection name of the string type.
Query: query a document. It is used to search documents.
Sort: the condition for sorting results.
Update: Modify the document to update the document.
Remove: boolean type, indicating whether to delete a document.
New: boolean type, indicating whether to return the document before update or after update. The default value is the document before update.
Update and remove must exist or exist. If no matching document exists, this command returns an error. This command has some limitations, that is, you can only process one document at a time, you cannot perform the upsert operation, you can only update existing documents.

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.