0
나는 mongodb beginner입니다.Java를 사용하여 mongodb 문서를 쿼리하는 중
문서로 ItemName 및 ItemCategory를 저장했습니다. Java 사용하기 메소드에서 전달 된 ItemCategory에 대한 모든 ItemNames를 찾고 싶다.
다음 코드를 시도했지만 작동하지 않습니다. 여기 generate()
방법
public String[] generate(String category) {
MongoClient mongoClient;
String temp[] = new String[50001];
String[] prodName = {};
int i = 0, j = 0;
int k=0;
try {
mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("test");
DBCollection coll = db.getCollection("products");
DBCursor cursor = coll.find();
BasicDBObject query = new BasicDBObject("ItemCategroy", category);
cursor = coll.find(query);
try {
while (cursor.hasNext()) {
temp[i++] = (String) cursor.next().get("ItemName");
}
for (k = 0; k < temp.length; k++) {
if (temp[k] == null) {
k++;
} else{
prodName[j++]=temp[k];
}
}
System.out.println("kkk"+k);
/* for (int j = 0; j < nodeNm.length; j++) {
System.out.println("printing in jsp " + nodeNm[j]);
} */
} finally {
cursor.close();
}
mongoClient.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return prodName;
}
나는 반환되는 배열
prodName
의 모든
ItemNames
을
category
을 전달하고 싶어하고있다.
제발 suugest.
샘플 문서가 같은 -'{ "_id"를 찾습니다 : ObjectId가을 ("528f37fdb55099000c5dfedd"), "항목 이름": "항목 1", "ItemCategory": "전자", "ItemSubCategory": "TV", "ItemBrand": "Samsung", "ItemSize": "20", "ItemPrice": "7500" } ' –
괜찮아요, 그래서 ""ItemCategroy "는 오타입니다. – chk