こんにちは、私は次の構造を持つ映画と呼ばれるコレクションを持っています
映画{ タイトル、ID、キャラクター[ {キャラクター名、俳優ID} ] }
actor_id
対応するID (映画)を使用して、特定の Character_name を照会しようとしています。映画の登場人物のリスト全体を取得せずに、これがどのように可能でしょうか? たとえば、私はすべてのCharacter_name
Brad Pittが ArrayList の形式で、各 movie ごとに再生したことを望んでいます。
映画からタイトルの ArrayList を作成するコードを次に示します。これを変更して個々のキャラクター名を取得するにはどうすればよいですか?
public static ArrayList popTitle() {
ArrayList myList = new ArrayList();
MongoClient mongoClient = new MongoClient(new ServerAddress(
"localhost", 27017));
DB db = mongoClient.getDB("test");
BasicDBObject query = new BasicDBObject();
query.put("Characters.actor_id", "162684857");
DBCollection myCollection = db.getCollection("Movies");
DBCursor cursor = myCollection.find(query);
while (cursor.hasNext()) {
DBObject theObj = cursor.next();
BasicDBObject title = (BasicDBObject) theObj;
String t = title.getString("title");
myList.add(t);
}
Set<String> s = new LinkedHashSet<>(myList);
myList.clear();
myList.addAll(s);
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i));
}
return myList;
}
これが私がこれまでに持っているものです。クエリ結果は null です。
public static void popCharacter() {
ArrayList myList = new ArrayList();
MongoClient mongoClient = new MongoClient(new ServerAddress(
"localhost", 27017));
DB db = mongoClient.getDB("test");
DBCollection myCollection = db.getCollection("Movies");
BasicDBObject query = new BasicDBObject();
query.put("id", "10015");
query.put("Characters.actor_id", "162684857");
DBCursor cursor = myCollection.find(query);
while (cursor.hasNext()) {
DBObject theObj = cursor.next();
BasicDBObject title = (BasicDBObject) theObj;
String t = title.getString("Characters.Character_name");
System.out.println(t);
}
}