简体   繁体   中英

In MongoDb is there a way to get last 1000 array elements from multiple documents

Given the following Mongo document model:

文件模型

Each document in the collection represents 1 hour of resource monitoring. In each document, there is a collection of summaries. There is also a count of the number of summaries as an integer, as it may make life easier.

Is there an efficient way to query the collection and return either just the recent most 1000 summaries as an aggregated list?

Or an efficient way to query the collection and return the number of documents that contain the recent most 1000 summaries?

The number of summaries in each document will differ, but the number of summaries in one single document will never equal more than 1000.

EDIT: I should mention I am using mongo with the .NET driver so have LINQ available to me.

Have you looked at Mongo aggregation ? If you want to return the 1000 most recent summaries you could go with an $unwind followed by a $replaceRoot operation, here's the shell query I tried :

db.getCollection('test').aggregate([
{$match : {your timestamp match query here}},
{$sort : {"timestamp": -1}},
{$unwind : "$summaries"},
{$limit : 1000},
{$replaceRoot: {newRoot: "$summaries"}}
])

The match operation at the beginning of your aggregation pipeline is important as indexes are only used at the first step. If you unwind your whole collection your performance might drop drastically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM