Java知识分享网 - 轻松学习从此开始!    

Java知识分享网

Java1234官方群25:java1234官方群17
Java1234官方群25:838462530
        
SpringBoot+SpringSecurity+Vue+ElementPlus权限系统实战课程 震撼发布        

最新Java全栈就业实战课程(免费)

springcloud分布式电商秒杀实战课程

IDEA永久激活

66套java实战课程无套路领取

锋哥开始收Java学员啦!

Python学习路线图

锋哥开始收Java学员啦!

MongoDB The Definitive Guide 3rd Edition PDF 下载


分享到:
时间:2020-06-06 14:59来源:http://www.java1234.com 作者:小锋  侵权举报
MongoDB The Definitive Guide 3rd Edition PDF 下载
失效链接处理
MongoDB The Definitive Guide 3rd Edition PDF 下载

本站整理下载:
 
相关截图:
 
主要内容:

case, the database name) and print it.
Access collections from the db variable. For example, db.movies returns the movies
collection in the current database. Now that we can access a collection in the shell, we can
perform almost any database operation.
Basic Operations with the Shell
We can use the four basic operations, create, read, update, and delete (CRUD) to manipulate and
view data in the shell.
CREATE
The insertOne function adds a document to a collection. For example, suppose we want to
store a movie. First, we’ll create a local variable called movie that is a JavaScript object
representing our document. It will have the keys "title", "director", and "year" (the
year it was release):
> movie = {"title" : "Star Wars: Episode IV ­ A New Hope",
..."director" : "George Lucas",
..."year" : 1977} {
"title" : "Star Wars: Episode IV ­ A New Hope",
"director" : "George Lucas",
"year" : 1977
}
This object is a valid MongoDB document, so we can save it to the movies collection using the
insertOne method:
> db.blog.insertOne(movie) {
Download from www.finelybook.com 7450911@qq.com 18
"acknowledged" : true,
"insertedId" : ObjectId("5721794b349c32b32a012b11") }
The movie has been saved to the database. We can see it by calling find on the collection:
> db.movies.find()
{
"_id" : ObjectId("5721794b349c32b32a012b11"),
"title" : "Star Wars: Episode IV ­ A New Hope",
"director" : "George Lucas",
"year" : 1977
}
You can see that an "_id" key was added and that the other key/value pairs were saved as we
entered them. The reason for the sudden appearance of the "_id" field is explained at the end
of this chapter.
READ
find and findOne can be used to query a collection. If we just want to see one document
from a collection, we can use findOne: > db.movies.findOne()
{
"_id" : ObjectId("5721794b349c32b32a012b11"),
"title" : "Star Wars: Episode IV ­ A New Hope",
"director" : "George Lucas",
"year" : 1977
}
find and findOne can also be passed criteria in the form of a query document. This will
restrict the documents matched by the query. The shell will automatically display up to 20
documents matching a find, but more can be fetched. See Chapter 4 for more information on
querying.
UPDATE
If we would like to modify our post, we can use updateOne. updateOne takes (at least) two
parameters: the first is the criteria to find which document to update, and the second is the new
document. Suppose we decide to enable reviews for our movie we created earlier. We’ll need to
add an array of reviews as the value for a new key in our document.
To perform the update, we’ll need to use an update operator, $set.
Download from www.finelybook.com 7450911@qq.com 19
> db.movies.update({title : "Star Wars: Episode IV ­ A New Hope"},
...{$set : {reviews: []}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
        
Now the document has a "reviews" key. If we call find again, we can see the new key:
> db.movies.find()
{
"_id" : ObjectId("5721794b349c32b32a012b11"),
"title" : "Star Wars: Episode IV ­ A New Hope",
"director" : "George Lucas",
"year" : 1977,
"reviews" : [ ] }
See “Updating Documents” for detailed information on updating documents.
DELETE
deleteOne and deleteMany permanently delete documents from the database. Both methods
take filter document specifying criteria for removal. For example, this would remove the movie
we just created:
> db.movies.deleteOne({title : "Star Wars: Episode IV ­ A New Hope"})
Use deleteMany to delete all documents matching a filter.
Data Types
The beginning of this chapter covered the basics of what a document is. Now that you are up
and running with MongoDB and can try things on the shell, this section will dive a little deeper.
MongoDB supports a wide range of data types as values in documents. In this section, we’ll
outline all the supported types.
Basic Data Types
Documents in MongoDB can be thought of as “JSON­like” in that they are conceptually similar
to objects in JavaScript. JSON is a simple representation of data: the specification can be
described in about one paragraph (their website proves it) and lists only six data types. This is a
good thing in many ways: it’s easy to understand, parse, and remember. On the other hand,
JSON’s expressive capabilities are limited because the only types are null, boolean, numeric,
string, array, and object.
Download from www.finelybook.com 7450911@qq.com 20
Although these types allow for an impressive amount of expressivity, there are a couple of
additional types that are crucial for most applications, especially when working with a database.
For example, JSON has no date type, which makes working with dates even more annoying than
it usually is. There is a number type, but only one—there is no way to differentiate floats and
integers, never mind any distinction between 32­bit and 64­bit numbers. There is no way to
represent other commonly used types, either, such as regular expressions or functions.
MongoDB adds support for a number of additional data types while keeping JSON’s essential
key/value pair nature. Exactly how values of each type are represented varies by language, but
this is a list of the commonly supported types and how they are represented as part of a
document in the shell. The most common types are

 

------分隔线----------------------------

锋哥公众号


锋哥微信


关注公众号
【Java资料站】
回复 666
获取 
66套java
从菜鸡到大神
项目实战课程

锋哥推荐