对文档中特定字段的MongoDB查询返回空文档

时间:2017-09-28 21:47:27

标签: mongodb

对于每个GSM用户,我在“服务”集合中有一个文档,我要查询的部分如下(不得不省略部分文档并将其分成两部分,因为它很长),我我试图获取documentVerifiedDate,其中documentPurposeserviceNumber等于特定值:

{
  "_id": ObjectId("59abee12e4b044ce2d6001b6"),
  "service": {
    "serviceRequestId": "1335102",
    "serviceIndex": "S0",
    "accountIndex": "A0",
    "serviceUser": {
      "isServiceUserSameAsCustomer": "Y",
      "isHolder": "N",
      "isPayer": "N",
      "profileDetails": {
        "identificationDetails": {
          "identificationDetail": [{
              "idType": {
                "masterCode": "PASS"
              },
              "documentPurpose": {
                "masterCode": "POID"
              },
              "idNumber": "9339904299",
              "isReceived": "Y",
              "isVerified": "Y",
              "documentReceivedDate": ISODate("2017-09-05T08:13:25.000+0000"),
              "DMSReferenceNo": "85449499",
              "documentVerifiedDate": ISODate("2017-09-05T08:13:25.000+0000")
            },
            {
              "idType": {
                "masterCode": "REGFORM"
              },
              "documentPurpose": {
                "masterCode": "REGFORM"
              },
              "isUploaded": "Y",
              "isReceived": "Y",
              "isVerified": "Y",
              "documentUploadedDate": ISODate("2017-09-05T08:13:25.000+0000"),
              "DMSReferenceNo": "85449499",
              "documentReceivedDate": ISODate("2017-09-05T08:13:25.000+0000"),
              "documentVerifiedDate": ISODate("2017-09-05T08:13:25.000+0000")
            },
            {
              "idType": {
                "masterCode": "MMS"
              },
              "documentPurpose": {
                "masterCode": "MMS"
              },
              "isUploaded": "Y",
              "isReceived": "Y",
              "isVerified": "Y",
              "documentUploadedDate": ISODate("2017-09-05T08:13:25.000+0000"),
              "DMSReferenceNo": "85449499",
              "documentReceivedDate": ISODate("2017-09-05T08:13:25.000+0000"),
              "documentVerifiedDate": ISODate("2017-09-05T08:13:25.000+0000")
            }
          ]
        },

还有:

"serviceDetails" : {
            "serviceNumberCategory" : {
                "masterCode" : "NORML"
            }, 
            "selfCareAccount" : "Y", 
            "contractDetails" : {
                "startDate" : ISODate("2017-09-03T11:56:58.658+0000"), 
                "endDate" : ISODate("9999-12-31T00:00:00.000+0000")
            }, 
            "technology" : {
                "masterCode" : "GSM"
            }, 
            "starterKitUpdated" : "Y", 
            "relatedProject" : "", 
            "businessType" : {
                "masterCode" : "Prepaid"
            }, 
            "mobileMoneyAccount" : "Y", 
            "activatedVia" : {
                "masterCode" : "SP"
            }, 
            "serviceType" : {
                "masterCode" : "GSM"
            }, 
            "subServiceType" : {
                "masterCode" : "Voice"
            }, 
            "serviceNumber" : "9339904299", 
            "simDetails" : {

我在下面写了一个查询来获取documentVerifiedDate,其中documentPurpose是POID,serviceNumber是9339904299:

db.Service.find ({
        "service.serviceDetails.serviceNumber": "9339902499",
        "service.serviceUser.profileDetails.identificationDetails.identificationDetail.0.documentPurpose.masterCode" : "POID"
    }, 
    {
        "service.serviceUsear.profileDetails.identificationDetails.identificationDetail.0.documentVerifiedDate" : 1,
         _id: 0
    })

我的结果低于结果:

{ 
    "service" : {

    }
}

如果您能帮助我理解为什么上述查询不能达到预期目的,我将不胜感激,即不返回documentVerifiedDate。

我使用下面的链接来编写查询。 SQL to MongoDB Mapping Chart

1 个答案:

答案 0 :(得分:1)

来自docs

  

$ elemMatch,$ slice和$是项目特定的唯一方式   要包含在返回数组中的元素。例如,你不能   使用数组索引项目特定的数组元素;例如{   “instock.0”:1}投影不会用第一个投影数组   元件。

使用$位置运算符。

这样的东西
db.Service.find ({
    "service.serviceDetails.serviceNumber": "9339904299",
    "service.serviceUser.profileDetails.identificationDetails.identificationDetail.documentPurpose.masterCode": "POID"
  },
  {
    "service.serviceUser.profileDetails.identificationDetails.identificationDetail.$": 1
  })

这将为您提供匹配的identificationDetail对象,您可以使用文档中的documentVerifiedDate

注意:查询中的serviceNumber与提供的文档不匹配。我调整了查询​​以使用文档中的serviceNumber。