0

There are more than 1000 documents i.e fruit objects. I need to get the latest document (Full Object) i.e with respect to plucked date for each fruit.Here you go with the example

{
    "fruit":"apple",
    "pluckedDate": "2017-05-05 18:29:03.232Z",
    "desc":[
        {
            "type" : 1,
            "size" : 0.0,
            "weight" : 0.0
        }, 
        {
            "type" : 2,
            "size" : 10.0,
            "weight" : 77.0
        }, 
        {
            "type" : 3,
            "size" : 10.0,
            "weight" : 42.0
        }
    ]
},{
    "fruit":"apple",
    "pluckedDate": "2017-05-06 18:29:03.232Z",
    "desc":[
        {
            "type" : 1,
            "size" : 0.0,
            "weight" : 0.0
        }, 
        {
            "type" : 2,
            "size" : 102.0,
            "weight" : 377.0
        }, 
        {
            "type" : 3,
            "size" : 103.0,
            "weight" : 142.0
        }
    ]
},{
    "fruit":"orange",
    "pluckedDate": "2017-05-05 18:29:03.232Z",
    "desc":[
        {
            "type" : 1,
            "size" : 0.0,
            "weight" : 0.0
        }, 
        {
            "type" : 2,
            "size" : 10.0,
            "weight" : 37.0
        }, 
    ]
},{
    "fruit":"orange",
    "pluckedDate": "2017-05-04 18:29:03.232Z",
    "desc":[
        {
            "type" : 1,
            "size" : 11.0,
            "weight" : 89.0
        }, 
        {
            "type" : 2,
            "size" : 54.0,
            "weight" : 567.0
        }, 
    ]
}

I need the final output to return the latest full document from the mongoDB as showing in the following example :

{
    "fruit":"apple",
    "pluckedDate": "2017-05-06 18:29:03.232Z",
    "desc":[
        {
            "type" : 1,
            "size" : 0.0,
            "weight" : 0.0
        }, 
        {
            "type" : 2,
            "size" : 102.0,
            "weight" : 377.0
        }, 
        {
            "type" : 3,
            "size" : 103.0,
            "weight" : 142.0
        }
    ]
},{
    "fruit":"orange",
    "pluckedDate": "2017-05-05 18:29:03.232Z",
    "desc":[
        {
            "type" : 1,
            "size" : 0.0,
            "weight" : 0.0
        }, 
        {
            "type" : 2,
            "size" : 10.0,
            "weight" : 37.0
        }, 
    ]
}

I tried with the aggregation but i see that the full document is not getting returned or may be my aggregation query is wrong.

or is there a way to achieve this other than aggregation in MongoDB

The aggregation query is as follows:

fruitCollect.aggregate([
    //query param
    {"$sort":
      {"plukedDate":1}
    },
    { "$group": {
        "_id": "$fruit",
        "plukedDate": { "$last": "$plukedDate" },

        }
    },{
        "$project":{
            "fruit":1,
            "plukedDate":1,
            "desc":1
        }
    }
]).exec()

I'm completely stuck, Any Help Appreciated.

K.Karthik
  • 7
  • 1
  • 4
  • please add your current aggregation query – felix Aug 09 '17 at 07:38
  • `$first` or `$last` are generally meaningless without applying `$sort` first. Either specify either `$first` or `$last` ( depending on sort order ) for each required field or use `$$ROOT` instead of naming each field individually. But you need to `$sort` first. – Neil Lunn Aug 09 '17 at 07:54
  • This is not duplicate, As you can see the DESC is a array of objects, – K.Karthik Aug 09 '17 at 08:34

0 Answers0