2017-01-23 86 views
-2
{ 
    "_id" : ObjectId("586aac4c8231ee0b98458045"), 
    "store_code" : NumberInt(10800), 
    "counter_name" : "R.N.Electric", 
    "address" : "314 khatipura road", 
    "locality" : "Khatipura Road (Jhotwara)", 
    "pincode" : NumberInt(302012), 
    "town" : "JAIPUR", 
    "gtm_city" : "JAIPUR", 
    "sales_office" : "URAJ", 
    "owner_name" : "Rajeev", 
    "owner_mobile" : "9828024073", 
    "division_mapping" : [//this contains only 1 element in every doc 
     { 
      "dvcode" : "cfc", 
      "dc" : "trade", 
      "beatcode" : "govindpura", 
      "fos" : { 
       "_id" : ObjectId("586ab8318231ee0b98458843"), 
       "loginid" : "9928483483", 
       "name" : "Arpit Gupta", 
       "division" : [ 
        "cfc", 
        "iron" 
       ], 
       "sales_office" : "URAJ", //office 
       "gtm_city" : "JAIPUR" //city 
      }, 
      "beat" : { 
       "_id" : ObjectId("586d372b39f64316b9c3cbd7"), 
       "division" : { 
        "_id" : ObjectId("5869f8b639f6430fe4edee2a"), 
        "clientdvcode" : NumberInt(40), 
        "code" : "cfc", 
        "name" : "Cooking & Fabric Care", 
        "project_code" : "usha-fos", 
        "client_code" : "usha", 
        "agent_code" : "v5global" 
       }, 
       "beatcode" : "govindpura", 
       "sales_office" : "URAJ", 
       "gtm_city" : "JAIPUR", 
       "active" : true, 
       "agency_code" : "v5global", 
       "client_code" : "USHA_FOS", 
       "proj_code" : "usha-fos", 
       "fos" : { 
        "_id" : ObjectId("586ab8318231ee0b98458843"), 
        "loginid" : "9928483483", 
        "name" : "Arpit Gupta", 
        "division" : [ 
         "cfc", 
         "iron" 
        ], 
        "sales_office" : "URAJ", 
        "gtm_city" : "JAIPUR" 
       } 
      } 
     } 
    ], 
    "distributor_mail" : "[email protected]", 
    "project_code" : "usha-fos", 
    "client_code" : "usha", 
    "agent_code" : "v5global", 
    "distributor_name" : "Sundeep Electrical" 
} 

我在division_mapping的数组中只有1个元素,我想查找其division_mapping中的dc是交易的那些文档。如何从mongo中的数组中找到某些东西

我曾尝试以下操作:

"division_mapping":{$elemMatch:{$eq:{"dc":"trade"}}}}) 

不知道我在做什么错。 //也许我必须展开阵列,但有没有其他方法?

回答

0

根据MongoDB的文档

$elemMatch操作者匹配包含与所有指定的查询 条件匹配的至少一个元素的数组 字段的文档。

根据上述说明仅检索文件,其DC IN division_mapping是贸易请尝试执行下文提到的查询

db.collection.find({division_mapping:{$elemMatch:{dc:'trade'}}}) 
相关问题