2016-03-16 47 views
2

我在MongoDB的下列那種JSON文件:MongoDB中取數組值

{ 
"_id": ObjectId("56e244828581920a1d32fdd5"), 
"name": "ValentinesCampaign1457669250012", 
"owner": "Ashis", 
"lines": [ 
    { 
    "name": "Apparels1457669250012", 
    "number": 19128, 
    "startDate": "4-5-2016:8", 
    "endDate": "2-12-2020:14", 
    "targeting": [ 
    { 
     "brand": "PEOPLE", 
     "channels": [ 
     "ch60", 
     "ch90", 
     "ch10", 
     "ch30", 
     "ch50" 
     ], 
     "keywords": [ 
     "dress", 
     "skirts", 
     "bags", 
     "stilletoes", 
     "perfume", 
     "purse" 
     ] 
    } 
    ], 
    "exclusions": [ 
    { 
     "channels": [ 
     "ch88", 
     "ch22", 
     "ch101" 
     ], 
     "keywords": [ 
     "gun", 
     "knife", 
     "pistol" 
     ] 
    } 
    ], 
    "creatives": [ 
    { 
     "url": "http://www.people.com/people/", 
     "weight": 0.44, 
     "startDate": "4-10-2016:6", 
     "endDate": "5-5-2019:3" 
    } 
    ] 
} 
    ] 
} 

,如果我查詢來獲取所有的名字線我用下面的查詢:

db.campaigns.find({ lines: { $elemMatch: {name :"Apparels1457669250012" } } } ).pretty() 

這給了我正確的答案,但我應該怎麼做才能得到目標品牌是人的線

即我想要其targets.brand是「人」的文件。

到目前爲止,我已經使用了以下查詢,但他們不工作

db.campaigns.find({ lines: { $elemMatch: {name :"Apparels1457669250012" ,targeting :{$elemMatch:{"brand":"people"}} }} } ).pretty() 

db.campaigns.find({ lines.$.targeting: { $elemMatch: {brand :"people" } } } ).pretty() 

他們兩個不工作,能有人建議我爲這個正確的查詢?

+0

可能的重複http://stackoverflow.com/questions/4433401/mongodb-nested-documents-searching或http://stackoverflow.com/questions/11489728/how-can-we-we-give-query- for-embedded-documents-through-java-driver –

+0

它不是一個普通的嵌入式文檔,它是一個數組中的文檔數組,我需要查詢裏面的數組。 –

+0

你可以嘗試使用[filters](https://docs.mongodb.org/manual/reference/operator/aggregation/filter/) –

回答

3

試試這個。

db.campaigns.find({ 
      lines:{ 
       $elemMatch: { 
        targeting: { 
          $elemMatch: { 
           brand: "PEOPLE" 
             } 
           } 
          } 
      } 
}) 

不知道是否嵌套工作。但值得一試!

+0

這不會搜索所需的特定'lines.name'字段。它將返回所有具有'targeting.brand'作爲PEOPLE的文檔。 –

+0

我認爲問題在於找到「定位 - >品牌是人的線條」。所以這應該工作。 :) – Kaushal

+1

如果你想額外的名稱字段,然後使用這一個: db.campaigns.find({lines:{$ elemMatch:{name:「Apparels1457669250012」},$ elemMatch:{targeting:{$ elemMatch:{品牌: 「PEOPLE」}}}}}).pretty() –

0

試試這個

db.campaigns.find({ lines.name :"Apparels1457669250012",lines.targeting.brand : "PEOPLE" }).pretty() 

例如 以下是我的收藏。運行子陣列的查詢將導致如下try this一個預期

> db.companies.find({"name" : "AdventNet","relationships.person.first_name" : "S 
ridhar"}).limit(1).pretty() 

> db.companies.find({"name" : "AdventNet"}).limit(1).pretty() 
{ 
     "_id" : ObjectId("52cdef7c4bab8bd675297d8b"), 
     "name" : "AdventNet", 
     "permalink" : "abc3", 
     "crunchbase_url" : "http://www.crunchbase.com/company/adventnet", 
     "homepage_url" : "http://adventnet.com", 
     "blog_url" : "", 
     "blog_feed_url" : "", 
     "twitter_username" : "manageengine", 
     "category_code" : "enterprise", 
     "number_of_employees" : 600, 
     "founded_year" : 1996, 
     "deadpooled_year" : 2, 
     "tag_list" : "", 
     "alias_list" : "Zoho ManageEngine ", 
     "email_address" : "[email protected]", 
     "phone_number" : "925-924-9500", 
     "description" : "Server Management Software", 
     "created_at" : ISODate("2007-05-25T19:24:22Z"), 
     "updated_at" : "Wed Oct 31 18:26:09 UTC 2012", 
     "overview" : "<p>AdventNet is now <a href=\"/company/zoho-manageengine\" 
title=\"Zoho ManageEngine\" rel=\"nofollow\">Zoho ManageEngine</a>.</p>\n\n<p>F 
ounded in 1996, AdventNet has served a diverse range of enterprise IT, networkin 
g and telecom customers.</p>\n\n<p>AdventNet supplies server and network managem 
ent software.</p>", 
     "image" : { 
       "available_sizes" : [ 
         [ 
           [ 
             150, 
             55 
           ], 
           "assets/images/resized/0001/9732/19732v1-max-150 
x150.png" 
         ], 
         [ 
           [ 
             150, 
             55 
           ], 
           "assets/images/resized/0001/9732/19732v1-max-250 
x250.png" 
         ], 
         [ 
           [ 
             150, 
             55 
           ], 
           "assets/images/resized/0001/9732/19732v1-max-450 
x450.png" 
         ] 
       ] 
     }, 
     "products" : [ ], 
     "relationships" : [ 
       { 
         "is_past" : true, 
         "title" : "CEO and Co-Founder", 
         "person" : { 
           "first_name" : "Sridhar", 
           "last_name" : "Vembu", 
           "permalink" : "sridhar-vembu" 
         } 
       }, 
       { 
         "is_past" : true, 
         "title" : "VP of Business Dev", 
         "person" : { 
           "first_name" : "Neil", 
           "last_name" : "Butani", 
           "permalink" : "neil-butani" 
         } 
       }, 
       { 
         "is_past" : true, 
         "title" : "Usabiliy Engineer", 
         "person" : { 
           "first_name" : "Bharath", 
           "last_name" : "Balasubramanian", 
           "permalink" : "bharath-balasibramanian" 
         } 
       }, 
       { 
         "is_past" : true, 
         "title" : "Director of Engineering", 
         "person" : { 
           "first_name" : "Rajendran", 
           "last_name" : "Dandapani", 
           "permalink" : "rajendran-dandapani" 
         } 
       }, 
       { 
         "is_past" : true, 
         "title" : "Market Analyst", 
         "person" : { 
           "first_name" : "Aravind", 
           "last_name" : "Natarajan", 
           "permalink" : "aravind-natarajan" 
         } 
       }, 
       { 
         "is_past" : true, 
         "title" : "Director of Product Management", 
         "person" : { 
           "first_name" : "Hyther", 
           "last_name" : "Nizam", 
           "permalink" : "hyther-nizam" 
         } 
       }, 
       { 
         "is_past" : true, 
         "title" : "Western Regional OEM Sales Manager", 
         "person" : { 
           "first_name" : "Ian", 
           "last_name" : "Wenig", 
           "permalink" : "ian-wenig" 
         } 
       } 
     ], 
     "competitions" : [ ], 
     "providerships" : [ 
       { 
         "title" : "DHFH", 
         "is_past" : true, 
         "provider" : { 
           "name" : "A Small Orange", 
           "permalink" : "a-small-orange" 
         } 
       } 
     ], 
     "total_money_raised" : "$0", 
     "funding_rounds" : [ ], 
     "investments" : [ ], 
     "acquisition" : null, 
     "acquisitions" : [ ], 
     "offices" : [ 
       { 
         "description" : "Headquarters", 
         "address1" : "4900 Hopyard Rd.", 
         "address2" : "Suite 310", 
         "zip_code" : "94588", 
         "city" : "Pleasanton", 
         "state_code" : "CA", 
         "country_code" : "USA", 
         "latitude" : 37.692934, 
         "longitude" : -121.904945 
       } 
     ], 
     "milestones" : [ ], 
     "video_embeds" : [ ], 
     "screenshots" : [ 
       { 
         "available_sizes" : [ 
           [ 
             [ 
               150, 
               94 
             ], 
             "assets/images/resized/0004/3400/43400v1 
-max-150x150.png" 
           ], 
           [ 
             [ 
               250, 
               156 
             ], 
             "assets/images/resized/0004/3400/43400v1 
-max-250x250.png" 
           ], 
           [ 
             [ 
               450, 
               282 
             ], 
             "assets/images/resized/0004/3400/43400v1 
-max-450x450.png" 
           ] 
         ], 
         "attribution" : null 
       } 
     ], 
     "external_links" : [ ], 
     "partners" : [ ] 
} 
> 
+0

不起作用,看到這裏面沒有嵌入doc doc,它是數組裏的docs數組。 –

0

你能。

db.campaigns.find({ 
    lines.name: 'Apparels1457669250012', 
    targeting: { $elemMatch: { brand: 'PEOPLE' } } 
},'targeting') 

OR

db.campaigns.find({ lines.name: "Apparels1457669250012"}, { targeting: { $elemMatch: { brand: "PEOPLE" } } }) 
+0

給出了以下錯誤: 2016-03-16T14:59:10.182 + 0530 E QUERY [thread1] SyntaxError:missing:屬性ID後@(shell):1:27 –

+0

@AnkitaBhowmik:錯誤是什麼,你可以嘗試另一個查詢...應該工作.. – Thalaivar