让我们看看如何执行简单 MongoDB 查询如 创建, 插, 更新, 删除, 下降
在本节中,我们将看到如何在MongoDB中创建数据库和集合。
创建MongoDB数据库
The use
Command
MongoDB use DATABASE_NAME
is used to make a database. The command will 创建 a new database if it does not exist, otherwise, it’ll return the present database.
用于创建新数据库的语法
use数据库语句的基本语法如下:
use DATABASE_NAME
例如,如果要使用数据库名称作为“ movie_collection”,则将使用以下语句
>use movie_collection
switched to db movie_collection
如果要检查当前使用的数据库。您可以使用以下语句–
>db
movie_collection
如果要检查数据库的所有列表,则可以使用以下命令
>show dbs
local 0.78125GB
test 0.23012GB
您创建的数据库(movie_collection)不在列表中。要显示数据库,您需要至少插入一个文档。
>db.movie.insert({"名称":"Crypto"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
The 创建Collection()
Method In MongoDB
for creating a collection in MongoDB the method 创建Collection(name, 选项s)
is used.
例如
db.createCollection(name, 选项s)
在上述方法中, 名称 是需要b创建的集合的名称。还有哪里 选项 是文档,用于指定集合的配置。
参数 | 类型 | 描述 |
---|---|---|
名称 | 串 | 馆藏名称 |
选件 | 文件 | (可选)指定有关内存大小和索引的选项 |
Following table shows the list of 选项s you can use in method 创建Collection(name, 选项s)
.
领域 | 类型 | 描述 |
---|---|---|
最高 | 数 | (可选)指定上限集合中允许的最大文档数。 |
尺寸 | 数 | (可选)指定上限集合的最大大小(以字节为单位)。如果capped为true,那么您想另外指定此字段。 |
autoIndexId | 布尔型 | (可选)如果为true,则自动在子字段上创建索引。s默认值为false。 |
封顶 | 布尔型 | (可选)如果为true,则允许上限收集。上限集合是已安装大小的集合,一旦达到最大大小,该集合将自动覆盖其最早的条目。如果指定true,则希望另外指定size参数。 |
MongoDB首先检查上限集合的大小字段,然后在 插入文件
例子
The basic syntax of the method 创建Collection()
without 选项s is as follows −
>use movie_collection
switched to db movie_collection
>db.createCollection("popular_movies")
{ "ok" : 1 }
The method 创建Collection()
without 选项s is as follows −
>db.createCollection("mycol", { 封顶:true, autoIndexId:true, 尺寸:6142800, 最高:10000 } )
{ "ok" : 1 }
>
在本节中,我们将看到如何在MongoDB中删除数据库和集合。
删除MongoDB数据库
The 下降Database()
Method
To 下降 an existing database in MongoDB db.dropDatabase()
method is used.
句法 -
db.dropDatabase()
以上方法将删除所选数据库。如果您尚未选择任何数据库,则它将删除默认的“测试”数据库。
The 下降()
Method In MongoDB
To 下降 an existing collection in MongoDB db.collection.drop()
method is used.
句法 -
>db.movie_collection.drop()
true
下降()
如果成功删除了选定的集合,则方法将返回true,否则将返回false。
在本节中,我们将看到如何在MongoDB中插入文档。
The 插()
Method In MongoDB
We can use 插()
or save()
method to 插 data into MongoDB collection
示例–
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: '//www.geekstrick.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
mycol是我们在上一章中创建的集合名称。如果数据库中不存在该集合,则MongoDB可以创建此集合,然后将文档插入其中。
在插入的文档中,如果我们未指定aid参数,则MongoDB会为此文档分配一个唯一的ObjectId。_id
是集合中每个文档唯一的十二个字节的十六进制数。十二个字节划分如下
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
传递文件数组
To 插 multiple documents in a single query, you can pass an array of documents in 插()
command.
示例–
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: '//www.geekstrick.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: '//www.geekstrick.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'Its a comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
To 插 the document you’ll be able to use db.post.save(document)
also. If you do not specify child in the document then save()
method will work same as 插()
method. If you specify child then it will replace whole data of document containing child as specified in save()
method.
MongoDB Update()
Method
MongoDB’s 更新()
and save()
methods are used to 更新 document into a collection. The 更新()
method 更新s the values in the existing document while the save()
method replaces the existing document with the document passed in save()
method.
例
考虑到mycol集合具有以下数据。
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
以下示例将为标题为“ MongoDB概述”的文档设置新标题“ New MongoDB Tutorial”。
>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
>
要更新多个文档,您需要将参数“ multi”设置为true。
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
The remove()
Method in MongoDB
在本节中,我们将看到如何使用MongoDB删除文档。
MongoDB’s remove()
method is used to remove a document from the collection. remove()
method accepts 2 parameters. One is deletion criteria and second is 只有一个 flag.
- 删除标准– (可选)根据文档的删除条件将被删除。
- 只有一个 - (可选)如果设置为true或1,则仅删除一个文档。
仅删除一个
如果有多个记录,并且您只想删除第一个记录,则进行设置 只有一个 parameter in remove()
method.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
删除所有文件
如果您未指定删除条件,则MongoDB将从集合中删除整个文档。这等效于SQL的truncate命令。
>db.mycol.remove()
>db.mycol.find()
>
获取更多文章 MongoDB