In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MongoDB shell operation data, using create, read, update, delete operation.
1. Create
The insert function is used to create a document into the collection.
For example, create a local variable post, which is a JavaScript object representing the document, with title, content, and date keys in it.
> post = {"title": "My Blog Post"
... "content": "Here's my blog post"
... "date": new Date ()}
{
"title": "My Blog Post"
"content": "Here's my blog post"
Date: ISODate ("2015-02-02T05:04:55.861Z")
}
> db
Test
Use the insert method to save to the collection blog, and note that blog does not exist at this time.
> db.blog.insert (post)
WriteResult ({"nInserted": 1})
2. Read
The find () function reads all the documents in the collection:
> db.blog.find ()
{"_ id": ObjectId ("54cf05c00eb7b5f5718da826"), "title": "My Blog Post", "conte"
Nt: "Here's my blog post", "date": ISODate ("2015-02-02T05:04:55.861Z")}
If you want to view only one document, use findOne ()
> db.blog.findone ()
2015-02-02T13:10:15.365+0800 TypeError: Property 'findone' of object test.blog I
S not a function
> db.blog.findOne ()
{
"_ id": ObjectId ("54cf05c00eb7b5f5718da826")
"title": "My Blog Post"
"content": "Here's my blog post"
Date: ISODate ("2015-02-02T05:04:55.861Z")
}
3. Update
3.1
Update accepts at least two parameters: one is to update the qualification of the document, and the other is the new document. Suppose you decide to add comments to a previously written article, you need to add a new key, and the corresponding value is the array where the comments are stored:
Modify the variable post and add the "comment" key:
> post.comments = []
[]
Execute update
> db.blog.update ({"title": "My Blog Post"}, post)
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.blog.find ()
{"_ id": ObjectId ("54cf05c00eb7b5f5718da826"), "title": "My Blog Post", "conte"
Nt ":" Here's my blog post "," date ": ISODate (" 2015-02-02T05:04:55.861Z ")," comm
Ents ": []}
> db.blog.findOne ()
{
"_ id": ObjectId ("54cf05c00eb7b5f5718da826")
"title": "My Blog Post"
"content": "Here's my blog post"
Date: ISODate ("2015-02-02T05:04:55.861Z")
"comments": []
}
3.2 use modifiers ("$inc" modifier)
Usually only part of the document needs to be updated, and the use of atomic update modifiers can make this partial update extremely efficient. Update modifiers are special keys that specify complex update operations, such as adjusting, adding, or deleting keys, as well as manipulating arrays or embedded documents.
Let's take a look at the updated example:
> db.people.find ()
{"_ id": ObjectId ("54d08f7f0eb7b5f5718da82a"), "name": "joe", "age": 65}
{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 20}
{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}
> db.people.update ({"age": 20}, {"$inc": {"age": 1}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.people.find ()
{"_ id": ObjectId ("54d08a1d0eb7b5f5718da828"), "name": "joe", "friends": 32
"enemies": 2}
{"_ id": ObjectId ("54d08f7f0eb7b5f5718da82a"), "name": "joe", "age": 65}
{"_ id": ObjectId ("54d08fb70eb7b5f5718da82b"), "name": "joe", "age": 21}
{"_ id": ObjectId ("54d08fbd0eb7b5f5718da82c"), "name": "joe", "age": 49}
>
4. Delete
Remove is used to permanently delete documents from the database, delete all documents in a collection if the parameter is not applicable, or accept a document to specify constraints:
> db.blog.remove ({"title": "My Blog Post"})
WriteResult ({"nRemoved": 1})
> db.blog.find ()
>
The deletion is permanent and cannot be undone or restored.
Deleting a document is usually quick, but to clear the entire collection, it is faster to delete the collection directly (and then re-index it).
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.