MongoDB查询数组中的上限子集合
在MongoDB中,您不能将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
]
}以下是查询数组中的上限子集合-
> db.demo319.aggregate([
... { $project: {TwoScores: { $slice: [ "$Scores", 2 ] } } }
... ])这将产生以下输出-
{ "_id" : ObjectId("5e50ecf6f8647eb59e562064"), "TwoScores" : [ 100, 345 ] }
{ "_id" : ObjectId("5e50ed01f8647eb59e562065"), "TwoScores" : [ 903, 10004 ] }热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志