주 문서의 다른 배열에 포함 된 배열 안에 특정 문서 만 반환 할 수있는 쿼리를 찾고 있습니다. 다음은 DB에 저장된 컬렉션입니다.
{
"List": {
"_id": "5ee1c1e6739bb23e3c54d35c",
"capitalAccountID": "CaptialAccount-43",
"userEmail": "[email protected]",
"flagID": 1,
"ledgerName": "Capital Account",
"subLedgers": [
{
"openingBalance": 100000,
"closingBalance": 90001,
"transactions": [
{
"_id": "5ee1c1e6739bb23e3c54d35e",
"transactionID": "123456",
"date": "09/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 0,
"debit": 1
},
{
"_id": "5ee1c1e6739bb23e3c54d35f",
"transactionID": "123457",
"date": "10/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 5000,
"debit": 0
},
{
"_id": "5ee1c1e6739bb23e3c54d360",
"transactionID": "123458",
"date": "11/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 5000,
"debit": 0
}
],
"_id": "5ee1c1e6739bb23e3c54d35d",
"subLedgerID": 1,
"subLedgerName": "ABC",
"reference": "dghj",
"rateOfDuty": 10,
"gstApplicable": true
},
{
"openingBalance": 100000,
"closingBalance": 95001,
"transactions": [
{
"_id": "5ee1c1e6739bb23e3c54d362",
"transactionID": "123459",
"date": "13/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 0,
"debit": 1
},
{
"_id": "5ee1c1e6739bb23e3c54d363",
"transactionID": "123450",
"date": "14/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 5000,
"debit": 0
},
{
"_id": "5ee1c1e6739bb23e3c54d364",
"transactionID": "123451",
"date": "15/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 5000,
"debit": 0
},
{
"_id": "5ee1c1e6739bb23e3c54d365",
"transactionID": "123452",
"date": "16/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 0,
"debit": 5000
}
],
"_id": "5ee1c1e6739bb23e3c54d361",
"subLedgerID": 2,
"subLedgerName": "DEF",
"reference": "dohj",
"rateOfDuty": 10,
"gstApplicable": true
}
],
"idCounter": 43,
"__v": 0
}
}
아래 코드는 위의 결과를 반환합니다.
await CaptialAccount.findOne(
{
userEmail: req.body.userEmail,
},
(err, list) => {
res.status(200).json({
List: list
})
}
);
날짜를 기준으로 "subLedgers"배열 내부의 "transactions"배열을 필터링해야합니다. 예 : "09/06/2020"에서 "13/06/2020"까지. 예상 출력 :
{
"List": {
"_id": "5ee1c1e6739bb23e3c54d35c",
"capitalAccountID": "CaptialAccount-43",
"userEmail": "[email protected]",
"flagID": 1,
"ledgerName": "Capital Account",
"subLedgers": [
{
"openingBalance": 100000,
"closingBalance": 90001,
"transactions": [
{
"_id": "5ee1c1e6739bb23e3c54d35e",
"transactionID": "123456",
"date": "09/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 0,
"debit": 1
},
{
"_id": "5ee1c1e6739bb23e3c54d35f",
"transactionID": "123457",
"date": "10/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 5000,
"debit": 0
},
{
"_id": "5ee1c1e6739bb23e3c54d360",
"transactionID": "123458",
"date": "11/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 5000,
"debit": 0
}
],
"_id": "5ee1c1e6739bb23e3c54d35d",
"subLedgerID": 1,
"subLedgerName": "ABC",
"reference": "dghj",
"rateOfDuty": 10,
"gstApplicable": true
},
{
"openingBalance": 100000,
"closingBalance": 95001,
"transactions": [
{
"_id": "5ee1c1e6739bb23e3c54d362",
"transactionID": "123459",
"date": "13/06/2020",
"particulars": "By-sales",
"voucherNumber": "asdfghjk",
"voucherType": "SR",
"credit": 0,
"debit": 1
}
],
"_id": "5ee1c1e6739bb23e3c54d361",
"subLedgerID": 2,
"subLedgerName": "DEF",
"reference": "dohj",
"rateOfDuty": 10,
"gstApplicable": true
}
],
"idCounter": 43,
"__v": 0
}
}
이를 달성하기위한 쿼리가 있습니까? 감사합니다.
이를 위해 $ filter를 활용하여 파이프 라인을 사용해야합니다 . $ elemMatch에 해당하는 쿼리 는 조건과 일치하는 배열의 첫 번째 항목을 반환하는 데만 사용할 수 있습니다.
db.collection.aggregate([
{
$match: {
userEmail: req.body.userEmail,
}
},
{
$addFields: {
"List.subLedgers": {
$map: {
input: "$List.subLedgers",
as: "subLedger",
in: {
$mergeObjects: ["$$subLedger", {
transactions: {
$filter: {
input: "$$subLedger.transactions",
as: "transaction",
cond: {
$and: [
{
$gte: ["$$transaction.date", "start date"]
},
{
$lte: ["$$transaction.date", "end date"]
}
]
}
}
}
}]
}
}
}
}
}
])
거래 날짜가 문자열로 저장되는 것처럼 보이지만 모두 동일한 시간대와 형식으로되어 있다는 것을 보장 할 수있는 한 좋지 않습니다. 그렇지 않으면 예기치 않은 결과를 얻을 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다