使用Java在Mongodb中进行复杂查询

时间:2015-06-29 05:48:34

标签: arrays mongodb-java

我是 mongodb 的新手,但是需要在java中做一些复杂的查询。我有一个数据格式如下

{
        "_id" : ObjectId("558d4887c33598835a7f5ee3"),
        "username" : "shree",
        "detail1" : "d1",
        "detail2" : "d2",
        "detail3" : "d3",
        "detail4" : [
                      {
                       "firstname":"Mary",
                       "lastname":"Smith",
                       "zipcode":"12345"
                      },
                      {
                       "firstname":"John",
                       "lastname":"Henry",
                       "zipcode":"54624"
                      },
                      {
                        "firstname":"Jacob",
                        "lastname":"Tell",
                        "zipcode":"19283"
                       },
                       {
                        "firstname":"William",
                        "lastname":"Dirk",
                        "zipcode":"15999"
                       }
                    ],

       "detail5" : "d5",

}

现在我想查询

1.检查“用户名”,如果匹配则在“detail4”中添加更多内容。

 {
        "_id" : ObjectId("558d4887c33598835a7f5ee3"),
        "username" : "shree",
        "detail1" : "d1",
        "detail2" : "d2",
        "detail3" : "d3",
        "detail4" : [
                      {
                       "firstname":"Mary",
                       "lastname":"Smith",
                       "zipcode":"12345"
                      },
                      {
                       "firstname":"John",
                       "lastname":"Henry",
                       "zipcode":"54624"
                      },
                      {
                        "firstname":"Jacob",
                        "lastname":"Tell",
                        "zipcode":"19283"
                       },
                       {
                        "firstname":"William",
                        "lastname":"Dirk",
                        "zipcode":"15999"
                       },
                       {
                       "firstname":"newFname",
                       "lastname":"newLname",
                       "zipcode":"newZipcode"
                       },
                    ],

       "detail5" : "d5",

}

2.检查“username = shree”以及“zipcode = 54624”,并且返回

  {
        "_id" : ObjectId("558d4887c33598835a7f5ee3"),
        "username" : "shree",
        "detail1" : "d1",
        "detail2" : "d2",
        "detail3" : "d3",
        "detail4" : {
                       "firstname":"John",
                       "lastname":"Henry",
                       "zipcode":"54624"
                     },

       "detail5" : "d5",

}

3.检查“username = shree”以及“zipcode = 15999”,并且 DELETE

   {
      "firstname":"William",
      "lastname":"Dirk",
      "zipcode":"15999"
   }

和数据库应保留:

{
        "_id" : ObjectId("558d4887c33598835a7f5ee3"),
        "username" : "shree",
        "detail1" : "d1",
        "detail2" : "d2",
        "detail3" : "d3",
        "detail4" : [
                      {
                       "firstname":"Mary",
                       "lastname":"Smith",
                       "zipcode":"12345"
                      },
                      {
                       "firstname":"John",
                       "lastname":"Henry",
                       "zipcode":"54624"
                      },
                      {
                        "firstname":"Jacob",
                        "lastname":"Tell",
                        "zipcode":"19283"
                       }
                    ],

       "detail5" : "d5",

}

仅回答Java代码,或者只需附加 链接 ,这是 Java

中此类查询的指南

0 个答案:

没有答案