MongoDB 阵列中 capped 子集合查询
在 MongoDB 中,您无法对子集合使用 capped。但是,对整体文档使用 capped。若要显示数组中的特定数量的值,请使用 $slice。
让我们创建一个包含文档的集合 -
> db.demo319.insertOne({"Scores":[100,345,980,890]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e50ecf6f8647eb59e562064")
}
> db.demo319.insertOne({"Scores":[903,10004,84575,844]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e50ed01f8647eb59e562065")
}在集合中显示所有文档,方法是使用 find() 方法 -
> db.demo319.find().pretty();
这将产生以下输出 -
{
"_id" : ObjectId("5e50ecf6f8647eb59e562064"),
"Scores" : [
100,
345,
980,
890
]
}
{
"_id" : ObjectId("5e50ed01f8647eb59e562065"),
"Scores" : [
903,
10004,
84575,
844
]
}以下是阵列中 capped 子集合的查询 -
> db.demo319.aggregate([
... { $project: {TwoScores: { $slice: [ "$Scores", 2 ] } } }
... ])这将产生以下输出 -
{ "_id" : ObjectId("5e50ecf6f8647eb59e562064"), "TwoScores" : [ 100, 345 ] }
{ "_id" : ObjectId("5e50ed01f8647eb59e562065"), "TwoScores" : [ 903, 10004 ] }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP