返回Json中与搜索字符串相同的嵌套元素 - mongodb

时间:2015-07-22 22:01:22

标签: java json mongodb

我在Mongo db中有以下json文档。 show元素将有几个季节元素,它们也有几个剧集元素,而这些元素又有多个questionEntry元素。

我想返回多个questionElements,其中questionElements metaTag条目等于我的搜索。例如。如果metaTag元素等于我的字符串,则返回它的父questionEntry元素并搜索嵌套在show中的所有元素。

{
"show":[
  {
     "season":[
        {
           "episodes":[
              {
                 "questionEntry":{
                    "id":1,
                    "info":{
                       "seasonNumber":1,
                       "episodeNumber":5,
                       "episodeName":"A Hero Sits Next Door"
                    },
                    "questionItem":{
                       "theQuestion":"What is the name of the ringer hired by Mr. Weed?",
                       "attachedElement":{
                          "type":1,
                          "value":""
                       }
                    },
                    "options":[
                       {
                          "type":1,
                          "value":"Johnson"
                       },
                       {
                          "type":1,
                          "value":"Hideo"
                       },
                       {
                          "type":1,
                          "value":"Guillermo"
                       }
                    ],
                    "answer":{
                       "questionId":1,
                       "answer":3
                    },
                    "metaTags":[
                       "Season 1",
                       "Episode 5",
                       "Trivia",
                       "Arya Stark",
                       "House Stark"
                    ]
                 }
              }
           ]
        }
     ]
   }
 ]
}

我在Windows 8.1中使用最新的Java Mongo驱动程序并使用Mongodb 2.4.4。所以我的问题是在整个show集合中返回与我的搜索字符串匹配的单个或多个qestionEntry元素的最佳方法是什么?

希望有人可以帮助我。

编辑:

private DB mongoDatabase;
private DBCollection mongoColl;
private DBObject dbObject;

// Singleton class
// Create client (server address(host,port), credential, options)
        mongoClient = new MongoClient(new ServerAddress(host, port), 
                Collections.singletonList(credential),
                options);

mongoDatabase = ClientSingleton.getInstance().getClient().getDB("MyDB");

1 个答案:

答案 0 :(得分:1)

请尝试以下方法:

 db.exp.aggregate([{"$redact":{"$cond": { if: {$gt:[ {"$size": {
 $setIntersection : [ { "$ifNull": [ "$metaTags", []]}, 
 ["House Stark"]]} } , 0 ]} , then:"$$PRUNE",
 else:"$$DESCEND" }}}]).pretty();

[OR]

编辑:

db.exp.aggregate([{"$unwind":"$show"},
 {"$unwind":"$show.season"},
 {"$unwind":"$show.season.episodes"},
 {"$match" : {"show.season.episodes.questionEntry.metaTags":{"$in": 
   ["Trivia"]}}},
 {"$group":{"_id":"$_id","episodes":{"$push":"$show.season.episodes"}
]);

JAVA CODE:

    MongoClient client = new MongoClient();
    List<String> continentList = Arrays.asList(new String[]{"Trivia"});
    DB db = client.getDB("example");
    DBCollection coll = db.getCollection("exp");
    DBObject matchFields = new 
       BasicDBObject("show.season.episodes.questionEntry.metaTags", 
      new BasicDBObject("$in", continentList));
    DBObject groupFields = new BasicDBObject( "_id",
       "$_id").append("episodes", 
       new BasicDBObject("$push","$show.season.episodes"));
    DBObject unwindshow = new BasicDBObject("$unwind","$show");
    DBObject unwindsea = new BasicDBObject("$unwind", "$show.season");
    DBObject unwindepi = new BasicDBObject("$unwind", 
      "$show.season.episodes");
    DBObject match = new BasicDBObject("$match", matchFields);
    DBObject group = new BasicDBObject("$group", groupFields);      
    AggregationOutput output = 
    coll.aggregate(unwindshow,unwindsea,unwindepi,match,group);

    for (DBObject result : output.results()) {
         System.out.println(result);
         }