Hello I have a collection called Movies with the following structure
Movies{ Title, Id, Characters[ {Character_name , actor_id} ] }
I am trying to query for a specific Character_name using a corresponding actor_id
and id (movie). How is this possible without getting the whole list of movie characters?
For example I want every Character_name
Brad Pitt has played per each movie , in the form of an ArrayList.
Here is my code for creating an ArrayList of titles from Movies, how can I change this to obtain individual character names?
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;
}
Here is what I have so far. The query results are 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);
}
}