我在MongoDB中有一个查询,它假定根据特定标准提取记录。我想我可能会误解聚合管道的功能。问题中的查询大部分是正确地拉动记录,但它引入了一些不符合标准的记录。它似乎是造成这个问题的查询的日期部分,我想我可能会做错了。任何帮助,将不胜感激。MongoDB聚合日期问题
查询:
var theFirst = new Date("2016", "0", "1", "0", "0", "0", "0");
var theLast = new Date("2017", "0", "1", "0", "0", "0", "0");
var query = [
{ $match: { $and: [{ "ProjectName": { '$regex': '^((?!win).)*$', '$options': 'i' } }, { "ProjectName": { '$regex': '^((?!Champion Cooler recommended).)*$', '$options': 'i' } }] } },
{ $match: { $or: [{ "Payments": { $exists: true } }, { "Reports": { $exists: true } }] } },
{ $match: { $or: [{ "Payments.ScheduledDate": { $lt: theLast, $gt: theFirst } }, { "Reports.ScheduledDate": { $lt: theLast, $gt: theFirst } }] } }, {
$lookup: {
from: 'winorganizations',
localField: 'OrganizationId',
foreignField: '_id',
as: 'orgitem'
}
}, {
$project: {
_id: 0,
OrgName: '$Organization',
Address1: '$Org_Info.Address1',
Address2: '$Org_Info.Address2',
Address3: '$Org_Info.Address3',
OrgCity: '$Org_Info.City',
OrgState: '$Org_Info.State',
OrgZip: '$Org_Info.Zip',
TaxID: '$Org_Info.TaxId',
orgitem: '$orgitem',
OrgContactName: "",
OrgContactEmailAddress: "",
GrantContactName: { "$arrayElemAt": ["$Contacts.Name", 0] },
GrantContactEmail: { "$arrayElemAt": ["$Contacts.Email", 0] },
GrantNbr: "$WINNbr",
AmtApproved: "$Amount",
DateAccepted: "$DateAccepted",
ProjectName: "$ProjectName",
ProgramArea: "$ProgramArea",
Initiative: "$Initiative",
Strategy: "$Strategy",
ProgramOfficer: "$programOfficer",
}
}
];
这适用于大部分除了第三行{ $match: { $or: [{ "Payments.ScheduledDate": { $lt: theLast, $gt: theFirst } }, { "Reports.ScheduledDate": { $lt: theLast, $gt: theFirst } }] } },
我想去的地方付款(嵌入式阵列)有greater than $gt
2016年1月1日和less than $lt
01/01指定日期的所有文件/ 2017或报告(嵌入式阵列)的预定日期为greater than $gt
01/01/2016和less than $lt
01/01/2017
它正在拉取文档,并在其中包含预定日期为12/30/2017这显然符合部分如果标准,但它喜欢它忽略$ lt。理想情况下,不应该返回此记录。
一方面,这是回报的文件有一个报告收集的是这样的:
"Reports" : [
{
"ProgramOfficer" : "eah",
"ProgramOfficerId" : "eah",
"ApprovedDate" : ISODate("2013-12-04T06:00:00.000Z"),
"Note" : "received emailed image",
"ReportTypeId" : "12",
"ReportType" : "ACK",
"ScheduledDate" : ISODate("2013-12-10T06:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "MH",
"ProgramOfficerId" : "MH",
"ApprovedDate" : ISODate("2014-11-03T06:00:00.000Z"),
"Note" : "Reports recevied and sent to MH 10.31.2014",
"ReportTypeId" : "6",
"ReportType" : "F,N.",
"ScheduledDate" : ISODate("2014-11-01T05:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "n/a",
"ProgramOfficerId" : "n/a",
"ApprovedDate" : null,
"Note" : "12.22.16 MH approves Second no-cost extension. First no-cost extension was due 2/1/2017. Original due date was 12/1/15. Umut Ozek asked MH for a 1 year extension for the term of the contract and a 2 year extension on the final report. See email in Report folder.",
"ReportTypeId" : "30",
"ReportType" : "FINAL",
"ScheduledDate" : ISODate("2017-12-31T06:00:00.000Z"),
"ReportStatus" : "Pending",
"ReportStatusId" : ""
},
{
"ProgramOfficer" : "TG",
"ProgramOfficerId" : "TG",
"ApprovedDate" : ISODate("2014-06-06T05:00:00.000Z"),
"Note" : "need original",
"ReportTypeId" : "12",
"ReportType" : "ACK",
"ScheduledDate" : ISODate("2014-01-10T06:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
}
]
之一的报告比2016年1月1日更大,但不低于01/01/2017.I需要查询才能拉到日期在这两个日期之间没有或两者之间的地点。
这是我希望返回的报告数组,如果我要使用$elemMatch
。正如你可以看到底部有一个报告是12/31/2016这应该符合我们的标准,但它不会返回。
`"Reports" : [
{
"ProgramOfficer" : "DKO",
"ProgramOfficerId" : "DKO",
"ApprovedDate" : ISODate("2007-03-19T05:00:00.000Z"),
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2007-03-31T05:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "",
"ProgramOfficerId" : "",
"ApprovedDate" : ISODate("2008-03-16T05:00:00.000Z"),
"Note" : "Gave to Nac",
"ReportTypeId" : "9",
"ReportType" : "N.",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "NGW",
"ProgramOfficerId" : "NGW",
"ApprovedDate" : ISODate("2009-01-16T06:00:00.000Z"),
"Note" : "Financial Activity Rpt",
"ReportTypeId" : "7",
"ReportType" : "F.",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "NGW",
"ProgramOfficerId" : "NGW",
"ApprovedDate" : ISODate("2009-03-31T05:00:00.000Z"),
"Note" : "2008 Annual Rpt",
"ReportTypeId" : "17",
"ReportType" : "ANNUAL",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "NGW",
"ProgramOfficerId" : "NGW",
"ApprovedDate" : ISODate("2010-01-08T06:00:00.000Z"),
"Note" : "",
"ReportTypeId" : "19",
"ReportType" : "UPDATE",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "n/a",
"ProgramOfficerId" : "n/a",
"ApprovedDate" : ISODate("2011-01-07T06:00:00.000Z"),
"Note" : "Gave to Nac",
"ReportTypeId" : "19",
"ReportType" : "UPDATE",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "NGW",
"ProgramOfficerId" : "NGW",
"ApprovedDate" : ISODate("2012-01-09T06:00:00.000Z"),
"Note" : "Investments down.",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "sn",
"ProgramOfficerId" : "sn",
"ApprovedDate" : ISODate("2014-01-02T06:00:00.000Z"),
"Note" : "n/a",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2013-12-31T06:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "NGW",
"ProgramOfficerId" : "NGW",
"ApprovedDate" : ISODate("2012-05-05T05:00:00.000Z"),
"Note" : "Annual report, overall foundation investment down from 2011.",
"ReportTypeId" : "17",
"ReportType" : "ANNUAL",
"ScheduledDate" : null,
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "n/a",
"ProgramOfficerId" : "n/a",
"ApprovedDate" : null,
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2014-12-31T06:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "AB",
"ProgramOfficerId" : "AB",
"ApprovedDate" : ISODate("2016-05-06T05:00:00.000Z"),
"Note" : "Received via email 5/6/16; Reviewed with SN",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2015-12-31T06:00:00.000Z"),
"ReportStatus" : "Complete",
"ReportStatusId" : "6eebae88-fca3-4b21-ad80-283eeb564f3f"
},
{
"ProgramOfficer" : "",
"ProgramOfficerId" : "",
"ApprovedDate" : null,
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2016-12-31T06:00:00.000Z"),
"ReportStatus" : "Pending",
"ReportStatusId" : ""
},
{
"ProgramOfficer" : "",
"ProgramOfficerId" : "",
"ApprovedDate" : null,
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2017-12-31T06:00:00.000Z"),
"ReportStatus" : "Pending",
"ReportStatusId" : ""
},
{
"ProgramOfficer" : "",
"ProgramOfficerId" : "",
"ApprovedDate" : null,
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2018-12-31T06:00:00.000Z"),
"ReportStatus" : "Pending",
"ReportStatusId" : ""
},
{
"ProgramOfficer" : "",
"ProgramOfficerId" : "",
"ApprovedDate" : null,
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2019-12-31T06:00:00.000Z"),
"ReportStatus" : "Pending",
"ReportStatusId" : ""
},
{
"ProgramOfficer" : "",
"ProgramOfficerId" : "",
"ApprovedDate" : null,
"Note" : "",
"ReportTypeId" : "26",
"ReportType" : "ENDOWMENT",
"ScheduledDate" : ISODate("2020-12-31T06:00:00.000Z"),
"ReportStatus" : "Pending",
"ReportStatusId" : ""
}
],`
请添加一个重现问题的示例文档。如果至少有一个付款或报告符合条件,则问题中的$匹配解析为真。 –
我添加了一个不应该被拉的集合。问题在于$匹配功能不正常,应该是$ lt和$ gt在一起,但它只能使用看起来像$ gt的一个。 – Ohjay44
该文档是否在Payments数组中有任何内容?它是一个'或'条件,这意味着如果任何一个数组有一个匹配的项目,则它解析为真。 –