Node.js 如何在mongoose中查找最近的子文档
我有一个名为Node.js 如何在mongoose中查找最近的子文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个名为RegisterList的mongoose文档,该文档包含名为Booking的子文档 这里我需要检索子文档,它是最近添加的子文档 下面是我的json数据 [ { _id: "56a3174bfc518cd014af7abd", area_name: "padi", name: "Vignesh", email: "vignesh4008@gmail.com", mobile_no: "928243
RegisterList
的mongoose文档,该文档包含名为Booking
的子文档
这里我需要检索子文档,它是最近添加的子文档
下面是我的json数据
[
{
_id: "56a3174bfc518cd014af7abd",
area_name: "padi",
name: "Vignesh",
email: "vignesh4008@gmail.com",
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: "ganesh@gmail.com",
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中
我们将非常感谢您的帮助
更新:
我需要为
和交货日期
和状态
查找和
条件的数据,因此如何编写mongoose查询以获取数据基于假设,您可以使用聚合框架获取最新的子文档
Mongodb版本3.2+
MongoDB版本<3.2
根据您提供的示例文档,输出如下所示
{
"_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" : "vignesh4008@gmail.com",
"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" : "ganesh@gmail.com",
"mobile_no" : "9042391491",
"date" : "2016-01-21T11:10:05.074Z"
}
不完全清楚你在问什么。您是指集合中每个文档的数组中最新的“
”排序值吗?或者您可能是指集合中“所有”文档中具有最新值的子文档?无论如何,在任何给定文档中,“最后一个”数组项始终是最新的,除非您特别在位置添加新项或在更新时对数组进行排序。您确切地说是指最新的“排序的”吗集合中每个文档的数组中的值如果通过$push
操作符将子文档推送到booking
数组中,它会将元素插入数组的末尾。除非在其中。因此,我们希望得到的使操作简单的澄清是,如果“最近的”总是在数组的末尾(默认情况下,没有其他修改),那么您真正需要的是从每个文档返回的数组的“最后”元素。当然,这里有一个内置的“投影”操作符,我也确信之前这里已经有人问过直接问题。您想在处订购最新的,可能是的副本吗?
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" : "vignesh4008@gmail.com",
"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" : "ganesh@gmail.com",
"mobile_no" : "9042391491",
"date" : "2016-01-21T11:10:05.074Z"
}