[英]How to return a filtered result of array inside a documents through a query in mongoose
I'm looking for a query that can return only certain document inside an array of a main document.我正在寻找一个只能返回主文档数组中的特定文档的查询。 Below is the collection stored in DB:以下是存储在 DB 中的集合:
{
"_id": "5ed49b42d6fc0c3878c875a2",
"stockID": "Stock-1",
"openingBalance": 30000,
"closingBalance": 29960,
"userEmail": "kumarshreyas073@gmail.com",
"stockName": "Coconut",
"unitMeasure": "Kgs",
"referenceName": "U16H",
"gstApplicable": true,
"rateOfDuty": 15,
"price": 30,
"total": 900000,
"idCounter": 1,
"transactions": [
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed630530de9f419fc45d2ed",
"transactionID": "5ed630530de9f419fc45d2eb",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed630c72218b03658bf8191",
"transactionID": "5ed630c62218b03658bf818f",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed64ebd89ee57327cd46772",
"transactionID": "5ed64ebd89ee57327cd46770",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed64f0cf5891925f0f0a8b1",
"transactionID": "5ed64f0cf5891925f0f0a8af",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed64f42b56b7d1380df8b70",
"transactionID": "5ed64f42b56b7d1380df8b6e",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed650674961b126a4092f3f",
"transactionID": "5ed650674961b126a4092f3d",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "U123",
"voucherType": "Purchase Return",
"inward": 0,
"outward": 10,
"_id": "5ed72df8f497fb0edc745c4b",
"transactionID": "CN-126656",
"date": "14/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "U123",
"voucherType": "",
"inward": 0,
"outward": 10,
"_id": "5ed73bf9c922270020db492d",
"transactionID": "CN-126665",
"date": "14/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "U123",
"voucherType": "Purchase Return",
"inward": 0,
"outward": 10,
"_id": "5ed753a85c61c71e3089f40f",
"transactionID": "CN-196700",
"date": "14/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "U123",
"voucherType": "Purchase Return",
"inward": 0,
"outward": 10,
"_id": "5ed753ed5c61c71e3089f415",
"transactionID": "CN-196000",
"date": "14/05/2020"
},
{
"particulars": "By demo5",
"voucherNumber": "1230456",
"voucherType": "Purchase",
"inward": 50,
"outward": 0,
"_id": "5ee735e807a0112c9c079d9a",
"transactionID": "5ee735e807a0112c9c079d98",
"date": "15/06/2020"
},
{
"particulars": "To demo",
"voucherNumber": "12345670",
"voucherType": "gdbxdkvghj",
"inward": 0,
"outward": 10,
"_id": "5ee9a4b2b8d4c72440e7a6a1",
"transactionID": "5ee9a33a6c1f103fa82992fd",
"date": "23/06/2020"
},
{
"particulars": "To demo1",
"voucherNumber": "1234567000",
"voucherType": "gdbxdkvghj",
"inward": 0,
"outward": 20,
"_id": "5ee9e28ef327db4358b7465d",
"transactionID": "5ee9e15cf327db4358b74651",
"date": "17/06/2020"
}
],
"__v": 0
}
The code Below returns above result:下面的代码返回上面的结果:
var stockAccount = await Stock.aggregate([
{
$match: {
userEmail: req.body.userEmail,
},
},
{
$match: {
"transactions.date": {
$gte: req.body.fromDate,
$lte: req.body.toDate,
},
},
},
]);
I need to filter the "transactions" array based on date.我需要根据日期过滤“交易”数组。 For example: From "09/05/2020" to "23/05/2020".例如:从“09/05/2020”到“23/05/2020”。 Expected Output:预期 Output:
{
"_id": "5ed49b42d6fc0c3878c875a2",
"stockID": "Stock-1",
"openingBalance": 30000,
"closingBalance": 29960,
"userEmail": "kumarshreyas073@gmail.com",
"stockName": "Coconut",
"unitMeasure": "Kgs",
"referenceName": "U16H",
"gstApplicable": true,
"rateOfDuty": 15,
"price": 30,
"total": 900000,
"idCounter": 1,
"transactions": [
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed630530de9f419fc45d2ed",
"transactionID": "5ed630530de9f419fc45d2eb",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed630c72218b03658bf8191",
"transactionID": "5ed630c62218b03658bf818f",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed64ebd89ee57327cd46772",
"transactionID": "5ed64ebd89ee57327cd46770",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed64f0cf5891925f0f0a8b1",
"transactionID": "5ed64f0cf5891925f0f0a8af",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed64f42b56b7d1380df8b70",
"transactionID": "5ed64f42b56b7d1380df8b6e",
"date": "23/05/2020"
},
{
"particulars": "To demo",
"voucherNumber": "1234567",
"voucherType": "Sales",
"inward": 0,
"outward": 10,
"_id": "5ed650674961b126a4092f3f",
"transactionID": "5ed650674961b126a4092f3d",
"date": "23/05/2020"
}
],
"__v": 0
}
Is there an query to achieve this?是否有查询来实现这一点? Thank you.谢谢你。
I believe aggregation is not needed in your case.我相信您的情况不需要聚合。
var stockAccount = await Stock.find({
userEmail: req.body.userEmail,
"transactions.date": {
$gte: req.body.fromDate,
$lte: req.body.toDate,
}
});
Note: It seems like the format of the date in the database is a string.注意:数据库中日期的格式似乎是一个字符串。 So, date comparison should not work.因此,日期比较不应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.