0

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);

    }   
}
Jam
  • 1
  • 1
  • Thanks BatScream. I have modified the question. My query results are null. – Jam Mar 12 '16 at 04:09
  • You would need to add a project statement in your query, `characters.$:1`, which would project only the matching array item and not the entire character array. – BatScream Mar 12 '16 at 04:18
  • Where should I place that statement in my code? – Jam Mar 12 '16 at 04:23
  • Thank you Blakes Seven but I am having trouble solving this using java. – Jam Mar 12 '16 at 04:26
  • Would you be able to help explain how I could translate JohnnHK's answer into java from the page you recommended? @BlakesSeven – Jam Mar 12 '16 at 05:42
  • That may be the "accepted" answer, but there are many more answers on the question. So it's basic "dot notation" to match the element ( as used ) and the positional `$` operator `"characters.$"` for the matched element in projection ( just like the answers say ). Then your result is a single array element, from which you reference the propery for the `"name"`. – Blakes Seven Mar 12 '16 at 05:49
  • I still do not understand how to perform the actual query in Java. @BlakesSeven – Jam Mar 12 '16 at 06:52

0 Answers0