【问题标题】:Retrieve multiple entries from 2 collections从 2 个集合中检索多个条目
【发布时间】:2014-05-07 12:34:54
【问题描述】:

场景:

我有两个 JSON 存储在 mongodb 中,格式如下:(它们来自 Yelp Educational)

JSON1:(Yelp 业务)

   {
     "business_id":"tl9XIP5trlkcuSfTQqe5jg",
     "full_address":"632 N Estrella Pkwy\nGoodyear, AZ 85338",
     "hours":{
   },
   "open":true,
   "categories":[
   "Fast Food",
   "Restaurants"
   ],
   "city":"Goodyear",
   "review_count":6,
   "name":"McDonalds",
   "neighborhoods":[

   ],
   "longitude":-112.39319500000001,
   "state":"AZ",
   "stars":2.0,
   "latitude":33.453887000000002,
   "attributes":{
   "Take-out":true,
   "Wi-Fi":"free",
   "Drive-Thru":true,
   "Alcohol":"none",
   "Caters":false,
   "Noise Level":"average",
   "Takes Reservations":false,
   "Delivery":false,
   "Parking":{
     "garage":false,
     "street":false,
     "validated":false,
     "lot":false,
     "valet":false
  },
  "Has TV":true,
  "Outdoor Seating":false,
  "Attire":"casual",
  "Waiter Service":false,
  "Accepts Credit Cards":true,
  "Good for Kids":true,
  "Price Range":1
   },
   "type":"business"
}

JSON2:(评论)

{
   "votes":{
   "funny":0,
   "useful":0,
   "cool":0
   },
   "user_id":"pNvoNTu6U7Ek2w_xe4QO-w",
   "review_id":"qyUlYgt68wexC_6qLL0sKg",
   "stars":1,
   "date":"2012-03-12",
   "text":"The worst McDonalds I've ever been to. The burgers are barely room temp and the cheese is barely melted on them even though they microwave them! Which is disgusting as it is. Chicken nuggets are always old and greasy (and again never hot enough). Filet o fish cold and gross..and either unmelted cheese or cheese that was nuked so long that it is like plastic. Nasty. I've never had a decent meal here. Haven't gone in months. Gross.",
   "type":"review",
   "business_id":"tl9XIP5trlkcuSfTQqe5jg"
}

他们都有相同的business_id

问题陈述:如何编写查询以便获取“类别”:“快餐”并同时获得评论?

我可以检索一个但不能检索评论。请发表评论!

代码:

System.out.println("Fast Food Restaurants");

BasicDBObject rest = new BasicDBObject();
rest.put("categories", "Indian");
DBCursor cursor2 = table.find(rest);
while(cursor2.hasNext()){ //display all fast food restaurants
    System.out.println(cursor2.next());
}

如何显示来自其他 JSON 的评分?

感谢您的宝贵时间!

【问题讨论】:

    标签: java json mongodb


    【解决方案1】:

    您必须分两步完成。在 Mongo 中没有连接。因此,您无法编写单个查询来访问来自两个不同集合的数据。

    您在以下步骤中获取了业务 -

    BasicDBObject rest = new BasicDBObject();
    rest.put("categories", "Indian");
    

    下一步 -

    DBObject query = new BasicDBObject();
    DBCursor cursor3 = null;
    DBObject dbObject = null;
    while(cursor2.hasNext()){ //display all fast food restaurants
        dbObject = cursor2.next();
        query.put("business_id",dbObject.get("business_id"))//get the business_id from dbObject returned from above
        cursor3 = table2.find(query); // here you have all the reviews for that business.
        //next loop through cursor3 for your reviews
    }
    

    在您的 mongodb 文档中,如果您将所有内容都存储为 String,那么您可以执行 dbObject.toString() 并获取 json,然后使用 gson 的 jackson 转换为 java pojo。

    【讨论】:

    • 已转发。谢谢你。我怎样才能在多个步骤中做到这一点?我一直在挠头,无法摆脱这个循环! :(
    • 我试过这样做,但有一件事我不明白。我想从第二个 JSON 中获取“文本”,它与第一个 json 具有相同的 business_id。我并没有真正在 JSON2 中获得评论(“文本”)(在我的示例中)我也尝试过 query.put(dbObject.get("business_id"),"text");
    • 你应该可以从 cursor3 获取它 - cursor3.next().get("text")
    • 非常感谢!我让它工作了。我如何限制top3的结果?就像我可以说System.out.println((cursor3.next().get("text")).limit(3)); 但这不起作用!
    猜你喜欢
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    相关资源
    最近更新 更多