Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java mongodb获取数组值_Java_Mongodb - Fatal编程技术网

Java mongodb获取数组值

Java mongodb获取数组值,java,mongodb,Java,Mongodb,我在mongodb中有以下类型的json文档: { "_id": ObjectId("56e244828581920a1d32fdd5"), "name": "ValentinesCampaign1457669250012", "owner": "Ashis", "lines": [ { "name": "Apparels1457669250012", "number": 19128, "startDate": "4-5-2016:8", "endDate": "2

我在mongodb中有以下类型的json文档:

{
 "_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()
这给了我正确的答案,但我应该怎么做才能得到目标品牌是人的路线

i、 e.我想要目标品牌为“人”的文件

到目前为止,我已经使用了以下查询,但它们不起作用

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

这两个都不起作用,有人能给我一个合适的查询吗?

试试这个

db.campaigns.find({ lines.name :"Apparels1457669250012",lines.targeting.brand : "PEOPLE" }).pretty()
例如。 下面是我的收藏。运行子数组查询将得到预期的结果

> 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" : "pr@adventnet.com",
        "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" : [ ]
}
>
>db.companys.find({“name”:“AdventNet”,“relationships.person.first_name”:“S
ridhar“}).limit(1).pretty()
>find({“name”:“AdventNet”}).limit(1.pretty())
{
“_id”:ObjectId(“52cdef7c4bab8bd675297d8b”),
“名称”:“AdventNet”,
“permalink”:“abc3”,
“crunchbase_url”:http://www.crunchbase.com/company/adventnet",
“主页地址”:http://adventnet.com",
“博客url”:“,
“blog_feed_url”:“”,
“twitter_用户名”:“manageengine”,
“类别代码”:“企业”,
“员工人数”:600人,
“成立年”:1996年,
“闲置年”:2,
“标签列表”:“,
“别名列表”:“Zoho ManageEngine”,
“电子邮件地址”:pr@adventnet.com",
“电话号码”:“925-924-9500”,
“说明”:“服务器管理软件”,
“创建时间”:ISODate(“2007-05-25T19:24:22Z”),
“更新时间”:“星期三10月31日18:26:09 UTC 2012”,
“概述”:“AdventNet现在是。

\n\nF AdventNet成立于1996年,为多种企业IT、networkin提供服务 g和电信客户。

\n\nAdventNet提供服务器和网络管理 ent软件。

“, “图像”:{ “可用尺寸”:[ [ [ 150, 55 ], “资产/图像/大小调整/0001/9732/19732v1-max-150 x150.png“ ], [ [ 150, 55 ], “资产/图像/大小调整/0001/9732/19732v1-max-250 x250.png“ ], [ [ 150, 55 ], “资产/图像/大小调整/0001/9732/19732v1-max-450 x450.png“ ] ] }, “产品”:[], “关系”:[ { “过去了吗”:是的, “头衔”:“首席执行官和联合创始人”, “人”:{ “名字”:“Sridhar”, “姓氏”:“Vembu”, “permalink”:“sridhar vembu” } }, { “过去了吗”:是的, “职务”:“业务发展副总裁”, “人”:{ “名字”:“尼尔”, “姓氏”:“布提”, “permalink”:“neil butani” } }, { “过去了吗”:是的, “职务”:“使用工程师”, “人”:{ “名字”:“巴拉斯”, “姓氏”:“Balasubramanian”, “permalink”:“bharath balasibramanian” } }, { “过去了吗”:是的, “职务”:“工程总监”, “人”:{ “名字”:“Rajendran”, “姓氏”:“Dandapani”, “permalink”:“rajendran dandapani” } }, { “过去了吗”:是的, “头衔”:“市场分析师”, “人”:{ “名字”:“阿拉文”, “姓氏”:“纳塔拉扬”, “permalink”:“aravind natarajan” } }, { “过去了吗”:是的, “职务”:“产品管理总监”, “人”:{ “名字”:“海瑟”, “姓氏”:“尼扎姆”, “permalink”:“hyther nizam” } }, { “过去了吗”:是的, “职务”:“西部地区OEM销售经理”, “人”:{ “名字”:“伊恩”, “姓氏”:“Wenig”, “permalink”:“ian wenig” } } ], “竞赛”:[], “供应商资格”:[ { “标题”:“DHFH”, “过去了吗”:是的, “提供者”:{ “名字”:“一个小橘子”, “permalink”:“一个小橘子” } } ], “募集资金总额”:“$0”, “融资回合”:[],
> 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" : "pr@adventnet.com",
        "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" : [ ]
}
>
db.campaigns.find({
  lines.name: 'Apparels1457669250012',
  targeting: { $elemMatch: { brand: 'PEOPLE' } }
},'targeting')
db.campaigns.find( { lines.name: "Apparels1457669250012"}, { targeting: { $elemMatch: { brand: "PEOPLE" } } })
db.campaigns.find({
          lines:{ 
              $elemMatch: {
                     targeting: {
                           $elemMatch: {
                                brand: "PEOPLE"
                                       }
                                }
                          }
            }
})