我正在寻找一个查询,该查询只能返回主文档数组内的某些文档。下面是存储在数据库中的集合:
{
"_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
}
下面的代码返回以上结果:
var stockAccount = await Stock.aggregate([
{
$match: {
userEmail: req.body.userEmail,
},
},
{
$match: {
"transactions.date": {
$gte: req.body.fromDate,
$lte: req.body.toDate,
},
},
},
]);
我需要根据日期过滤“交易”数组。例如:从“ 09/05/2020”到“ 23/05/2020”。预期输出:
{
"_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
}
是否有查询来实现?谢谢。
答案 0 :(得分:0)
我认为您的情况不需要汇总。
var stockAccount = await Stock.find({
userEmail: req.body.userEmail,
"transactions.date": {
$gte: req.body.fromDate,
$lte: req.body.toDate,
}
});
注意:数据库中日期的格式似乎是字符串。因此,日期比较不起作用。