我想检索数据库placedb中我的表位置中的地名。
我在数据库中的数据采用以下格式
**{ "_id" : ObjectId("514c23udhgfhdfge"), "placeList" : "chennai,coimbatore,madurai........", "state" : "tamilnadu" }**
下面是我尝试过的代码,但没有显示任何输出,我也不知道出了什么问题
MongoClient client = new MongoClient("localhost",27017); //with default server and port adress
DB db = client.getDB("placedb");
DBCollection areaColl=db.getCollection("places");
DBCursor find=areaColl.find();
String places = "";
while(find.hasNext())
{
places=find.next().get("placeList").toString();
}
System.out.println(places);
String[] place=places.split(",");
for(int i=0;i<place.length;i++)
{
System.out.println(place[i]);
}
最佳答案
Chk this .....................................................
MongoURI uri = new MongoURI("mongodb://ip of server");
Mongo mongo = (new Mongo.Holder()).connect(uri);
DB db = mongo.getDB("placeDB");
DBCollection coll = db.getCollection("places");
DBCursor cursor = coll.find();
JSONArray placeList = new JSONArray();
String places = "";
while (cursor.hasNext()) {
places = cursor.next().get("placeList").toString();
}
String[] place = places.split(",");
for (int i = 0; i < place.length; i++) {
placeList.add(place[i]);
}
System.out.println(placeList);