2017-05-15 177 views
0

我在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" : "" 
    } 
],` 
+0

請添加一個重現問題的示例文檔。如果至少有一個付款或報告符合條件,則問題中的$匹配解析爲真。 –

+0

我添加了一個不應該被拉的集合。問題在於$匹配功能不正常,應該是$ lt和$ gt在一起,但它只能使用看起來像$ gt的一個。 – Ohjay44

+0

該文檔是否在Payments數組中有任何內容?它是一個'或'條件,這意味着如果任何一個數組有一個匹配的項目,則它解析爲真。 –

回答

0

由於@Veeram,答案很簡單。我需要使用$elemMatch,並且在使用時我需要將嵌入式數組視爲查詢中的迭代對象。

最後的工作代碼:

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": {$elemMatch: {"ScheduledDate": { $lt: theLast, $gte: theFirst } } } }, { "Reports": {$elemMatch: {"ScheduledDate": { $lt: theLast, $gte: 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", 
    } 
} 
]; 

在第三行,我用$elemMatch和工作你會看到。