2016-03-05 66 views
1

我有稱爲RegisterList的貓鼬文檔,該文檔包含名爲Booking的子文檔。如何在貓鼬中找到最新的子文檔

在這裏,我需要找回子文檔,這是最近添加的子文檔

下面是我的JSON數據

[ 
    { 
     _id: "56a3174bfc518cd014af7abd", 
     area_name: "padi", 
     name: "Vignesh", 
     email: "[email protected]", 
     mobile_no: "9282438685", 
     otp: "1625", 
     __v: 0, 
     date: "2016-01-23T06:01:47.450Z", 
     booking: [ 
      { 
       name: "Vignesh", 
       mobile: "9282438685", 
       can_name: "Kinley", 
       can_quantity: "2", 
       can_cost: "80", 
       can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png", 
       delivery_date: "23-01-2016", 
       delivery_timeslot: "3pm-8pm", 
       order_id: "S16064", 
       subscription: "true", 
       subscription_type: "EveryDay", 
       total_cost: "560", 
       address: "12,Ramanrajan street,,padi,Chennai", 
       _id: "56a3174bfc518cd014af7abe", 
       delivered_at: "2016-01-22T18:30:00.000Z", 
       ordered_at: "2016-01-23T06:01:47.451Z", 
       status: "Delivered" 
      }, 
      { 
       name: "Vignesh", 
       mobile: "9282438685", 
       can_name: "Kinley", 
       can_quantity: "2", 
       can_cost: "80", 
       can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png", 
       delivery_date: "24-01-2016", 
       delivery_timeslot: "3pm-8pm", 
       address: "12,Ramanrajan street,,padi,Chennai", 
       order_id: "S16064", 
       subscription_type: "EveryDay", 
       _id: "56a31ba2d55894ec15eac1cf", 
       ordered_at: "2016-01-23T06:20:18.479Z", 
       status: "UnderProcess" 
      } 
     ] 
    }, 
    { 
     _id: "56a0bc8d3306f388131e56c6", 
     area_name: "kodambakkam", 
     name: "Ganesh", 
     email: "[email protected]", 
     mobile_no: "9042391491", 
     otp: "7828", 
     __v: 0, 
     date: "2016-01-21T11:10:05.074Z", 
     booking: [ 
      { 
       name: "Ganesh", 
       mobile: "9042391491", 
       can_name: "Bisleri", 
       can_quantity: "5", 
       can_cost: "250", 
       can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png", 
       delivery_date: "23-01-2016", 
       delivery_timeslot: "3pm-8pm", 
       order_id: "S12348", 
       subscription: "true", 
       subscription_type: "Alternate", 
       total_cost: "1000", 
       address: "15/A,Main Street,kodambakkam,Chennai", 
       _id: "56a3164dc2c549e811c0d08f", 
       delivered_at: "2016-01-22T18:30:00.000Z", 
       ordered_at: "2016-01-23T05:57:33.169Z", 
       status: "Delivered" 
      }, 
      { 
       name: "Ganesh", 
       mobile: "9042391491", 
       can_name: "Bisleri", 
       can_quantity: "5", 
       can_cost: "250", 
       can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png", 
       delivery_date: "25-01-2016", 
       delivery_timeslot: "3pm-8pm", 
       address: "15/A,Main Street,kodambakkam,Chennai", 
       order_id: "S12348", 
       subscription_type: "Alternate", 
       _id: "56a31c29d55894ec15eac1d0", 
       ordered_at: "2016-01-23T06:22:33.307Z", 
       status: "UnderProcess" 
      } 
     ] 
    } 
] 

我如何才能找到最近插入的子文檔單獨。在給定的JsonCode中。

幫助將不勝感激......

更新時間:

我需要找到爲AND條件數據delivery_datestatus,所以,我怎麼能寫貓鼬查詢來獲取數據

+0

不完全清楚你在問什麼。你的意思是集合中每個文檔的陣列中最新的「ordered_at」值?或者,您可能是指集合中具有**「全部」**文檔的最新值的子文檔?無論如何,在任何給定的文檔中,「最後」數組項目將始終是最新的,除非您專門在位置添加新項目或在更新時對數組進行排序。 –

+0

準確**您是否意味着數組中最近的「ordered_at」值對於集合中的每個文檔** – Nodemon

+0

如果通過'$ push'運算符將subdocument推送到'booking'數組,它會將元素插入到數組的末尾陣列。除非它在['$ position'](https://docs.mongodb.org/manual/reference/operator/update/position/)中。 – zangw

回答

1

基於假設,您可以使用聚合框架獲取最新的子文檔。

MongoDB的版本3.2+

db.col.aggregate([ 
{$project: { 
    "area_name" : 1, 
    "name" : 1, 
    "email" : 1, 
    "mobile_no" :1, 
    "otp" : 1, 
    "__v" : 1, 
    "date" : 1, 
    "booking": {$slice :["$booking",-1]}}} 
]) 

MongoDB的版本< 3.2

db.col.aggregate([ 
    // Stage 1 
    { 
     $unwind: "$booking" 
    }, 

    // Stage 2 
    { 
     $sort: { 
     "booking.ordered_at":-1 
     } 
    }, 

    // Stage 3 
    { 
     $group: { 
     _id: { 
     id: "$_id", 
     "area_name" : "$area_name", 
     "name" : "$name", 
     "email" : "$email", 
     "mobile_no" :"$mobile_no", 
     "otp" : "$opt", 
     "date" : "$date" 
     }, 
     booking:{$first: "$booking"} 
     } 
    }, 

    // Stage 4 
    { 
     $project: { 
     _id: 0, 
     _id: "$_id.id", 
     "area_name" : "$_id.area_name", 
     "name" : "$_id.name", 
     "email" : "$_id.email", 
     "mobile_no" :"$_id.mobile_no", 
     "otp" : "$_id.opt", 
     "date" : "$_id.date", 
     "booking": 1 
     } 
    } 
    ]); 

根據您提供的樣本文件,輸出會看起來像

{ 
    "_id" : "56a3174bfc518cd014af7abd", 
    "booking" : { 
     "name" : "Vignesh", 
     "mobile" : "9282438685", 
     "can_name" : "Kinley", 
     "can_quantity" : "2", 
     "can_cost" : "80", 
     "can_path" : "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png", 
     "delivery_date" : "24-01-2016", 
     "delivery_timeslot" : "3pm-8pm", 
     "address" : "12,Ramanrajan street,,padi,Chennai", 
     "order_id" : "S16064", 
     "subscription_type" : "EveryDay", 
     "_id" : "56a31ba2d55894ec15eac1cf", 
     "ordered_at" : "2016-01-23T06:20:18.479Z", 
     "status" : "UnderProcess" 
    }, 
    "area_name" : "padi", 
    "name" : "Vignesh", 
    "email" : "[email protected]", 
    "mobile_no" : "9282438685", 
    "date" : "2016-01-23T06:01:47.450Z" 
} 
{ 
    "_id" : "56a0bc8d3306f388131e56c6", 
    "booking" : { 
     "name" : "Ganesh", 
     "mobile" : "9042391491", 
     "can_name" : "Bisleri", 
     "can_quantity" : "5", 
     "can_cost" : "250", 
     "can_path" : "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png", 
     "delivery_date" : "25-01-2016", 
     "delivery_timeslot" : "3pm-8pm", 
     "address" : "15/A,Main Street,kodambakkam,Chennai", 
     "order_id" : "S12348", 
     "subscription_type" : "Alternate", 
     "_id" : "56a31c29d55894ec15eac1d0", 
     "ordered_at" : "2016-01-23T06:22:33.307Z", 
     "status" : "UnderProcess" 
    }, 
    "area_name" : "kodambakkam", 
    "name" : "Ganesh", 
    "email" : "[email protected]", 
    "mobile_no" : "9042391491", 
    "date" : "2016-01-21T11:10:05.074Z" 
}