2014-03-28 19 views
0
檢索多個條目

場景:從2個集

我有兩個JSON的存儲在以下格式的MongoDB的:(他們是Yelp的教育)

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的收視率?

謝謝你的時間!

回答

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的文件,如果你存儲的一切作爲字符串,那麼你可以做dbObject.toString ()並獲取json,然後使用gson的jackson轉換爲java pojo。

+0

Repped。謝謝。我怎樣才能做到多個步驟?我一直在撓我的頭一陣子,無法走出這個循環! :( –

+1

我已編輯帖子保存格式 – hellboy

+0

哇..我會試試這個並回復給你!!謝謝! –