How to query only documents with the latest timestamp from a group?

前端 未结 2 439
情话喂你
情话喂你 2020-12-20 10:44

In the MongoDB collection I\'m querying, each document represents an item at a specific time. When updating a document, a new document is created with the same item id and a

相关标签:
2条回答
  • 2020-12-20 10:47

    One can consider changing the document's schema to better suit your queries, and reduce the overhead of aggregation. Instead of creating a new document for each revision, you could push revision sub-documents onto an array and maintain the latest revision at the parent document; for example:

    {
        _id: x,
        itemId: 123,
        createdOn: ISODate("2014-02-09T14:26:20.102Z"),
        field1: "baz",
        field2: "fiz,
        revisions: [
            {createdOn: ISODate("2013-01-30T11:16:20.102Z"), field1: "foo", field2: "bar"},
            {createdOn: ISODate("2014-02-09T14:26:20.102Z"), field1: "baz", field2: "fiz"}
        ]
    }
    

    Keep in mind that MongoDB enforces a document-size limit of 16MB; this should suffice for most use cases. This would make your queries very simple: db.collection.find({field1: "foo"})

    Just another approach...

    0 讨论(0)
  • 2020-12-20 10:53

    You are mixing a few methods here when you can be doing everything in the aggregation pipeline. Otherwise it's just a matter of getting your steps in the right order:

    db.collection.aggregate([
        {$sort: { createdOn: -1 }},
        {$group: { _id: "$itemId", 
            createdOn: {$first: "$createdOn"},
            field1: {$first: "$field1" },
            field2: {$first: "$field2" }
        }},
        {$match: { field1: "foo" }}
    ])
    

    So sort first for newest documents. Group on the itemId ( order will be maintained for $first ), and then filter with $match if you must. But your grouped documents will be latest ones.

    0 讨论(0)
提交回复
热议问题