文档菜单
文档首页
/ / /
Ruby MongoDB 驱动
/

集合

本页内容

  • 时间序列集合
  • 固定集合
  • 将现有集合转换为固定集合
  • 文档验证
  • 向现有集合添加验证
  • 列出集合
  • 删除集合

MongoDB 在集合中存储文档。如果集合不存在,MongoDB 在您第一次向该集合中插入文档时创建该集合。

您还可以明确使用各种选项创建集合,例如设置最大大小或文档验证规则。

时间序列集合在MongoDB 5.0中被添加。您可以在以下链接中阅读文档这里.

时间序列集合有效地存储一段时间内测量的序列。时间序列数据是指随着时间的推移而收集的数据,它通过一个或多个不变的参数唯一标识。标识您的数据序列的不变参数通常是您的数据源的元数据。

为了创建时间序列集合,您必须明确使用时间序列选项创建集合

opts = {
time_series: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
},
expire_after: 604800
}
db['weather', opts].create

在创建时间序列集合时,指定以下选项

字段
描述
time_series[:timeField]
必需。包含每个时间序列文档中日期的字段名称。
time_series[:metaField]
可选。包含每个时间序列文档中元数据的字段名称。指定字段中的元数据应该是用于标记唯一文档序列的数据。元数据很少会改变。
time_series[:granularity]
可选。可能的值是 "seconds","minutes" 和 "hours"。默认情况下,MongoDB将粒度设置为 "seconds" 以支持高频率的摄取。
:expireAfterSeconds
可选。通过指定文档在时间序列集合中过期的秒数来启用自动删除文档。MongoDB会自动删除过期的文档。

有关时间序列集合选项的更多信息,请参阅MongoDB 文档

将数据插入到时间序列集合中与插入到常规集合类似

db['weather'].insert_many([
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 18, 0, 0, 0),
temp: 12
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 18, 4, 0, 0),
temp: 11
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 18, 8, 0, 0),
temp: 11
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 18, 12, 0, 0),
temp: 12
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 18, 16, 0, 0),
temp: 16
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 18, 20, 0, 0),
temp: 15
}, {
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 19, 0, 0, 0),
temp: 13
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 19, 4, 0, 0),
temp: 12
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 19, 8, 0, 0),
temp: 11
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 19, 12, 0, 0),
temp: 12
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 19, 16, 0, 0),
temp: 17
},
{
metadata: { sensorId: 5578, type: "temperature" },
timestamp: Time.utc(2021, 5, 19, 20, 0, 0),
temp: 12
}
])

查询时间序列集合也非常类似于常规集合

weather.find(timestamp: Time.utc(2021, 5, 18, 0, 0, 0)).first

此查询的结果

{
"timestamp" => 2021-05-18 00:00:00 UTC,
"metadata" => {
"sensorId" => 5578,
"type" => "temperature"
},
"temp" => 12,
"_id" => BSON::ObjectId('624dfb87d1327a60aeb048d2')
}

聚合管道还可以用于额外的查询功能

weather.aggregate([
{
"$project": {
date: {
"$dateToParts": { date: "$timestamp" }
},
temp: 1
}
},
{
"$group": {
_id: {
date: {
year: "$date.year",
month: "$date.month",
day: "$date.day"
}
},
avgTmp: { "$avg": "$temp" }
}
}
]).to_a

以下示例聚合管道根据测量日期对所有文档进行分组,然后返回当天所有温度测量的平均值

[{
"_id" => {
"date" => {
"year" => 2021,
"month" => 5,
"day" => 18
}
},
"avgTmp" => 12.833333333333334
},
{
"_id" => {
"date" => {
"year" => 2021,
"month" => 5,
"day" => 19
}
},
"avgTmp" => 12.833333333333334
}]

有关更多信息,请参阅MongoDB关于时间序列集合的文档。

有限集合具有最大大小或文档计数,防止其超过最大阈值。所有有限集合都必须指定最大大小,也可以指定最大文档计数。如果集合在达到最大文档计数之前达到最大大小限制,MongoDB会删除较旧的文档。

要创建一个有限集合,请使用带有字节大小的sizecapped: true选项。

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
collection = client[:artists, capped: true, size: 10000]
collection.create
collection.capped? # => true

要将现有集合从非有限集合转换为有限集合,请使用convertToCapped命令。

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
db = client.database
db.command({ 'convertToCapped' => 'artists', 'size' => 10000 })

如果您使用的是MongoDB 3.2或更高版本,您可以使用文档验证。具有验证的集合将每个插入或更新的文档与验证器选项中指定的标准进行比较。根据validationLevelvalidationAction,如果文档未满足指定的标准,MongoDB要么返回警告,要么拒绝插入或更新文档。

以下示例创建了一个具有验证器的 contacts 集合,该验证器指定插入或更新的文档应至少符合以下三个条件之一

  • phone 字段是一个字符串

  • email 字段匹配正则表达式

  • status 字段是 UnknownIncomplete

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
client[:contacts,
{
'validator' => { '$or' =>
[
{ 'phone' => { '$type' => "string" } },
{ 'email' => { '$regex' => /@mongodb\.com$/ } },
{ 'status' => { '$in' => [ "Unknown", "Incomplete" ] } }
]
}
}
].create

要向现有集合添加文档验证标准,请使用 collMod 命令。以下示例演示了如何向 contacts 集合添加验证,确保所有新文档都必须包含一个数字类型的 age 字段。

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
db = client.database
db.command({ 'collMod' => 'contacts',
'validator' =>
{ 'age' =>
{ '$type' => "number" }
}
})

使用数据库对象的 collectionscollection_names 方法列出集合

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
database = client.database
database.collections # Returns an array of Collection objects.
database.collection_names # Returns an array of collection names as strings.

要删除集合,请在集合对象上调用 drop 方法。

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
artists = client[:artists]
artists.drop

返回

数据库