Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve sub-document in array as DBObject(s)

I'm very new to MongoDB, and I'm using it along with the Java driver. I have this document structure:

{ "_id" : ObjectId("4f7d2ba6fd5a306d82687d48"), "room" : "Den" }
{ "_id" : ObjectId("4f7d2baafd5a306d82687d49"), "room" : "Foyer" }
{ "_id" : ObjectId("4f7d2fdcfd5a306d82687d4a"), "room" : "Master Bedroom" }
{ "_id" : ObjectId("4f7d301afd5a306d82687d4b"), "room" : "Guest Bedroom" }
{ "_id" : ObjectId("4f7d2b98fd5a306d82687d47"), "code" : "A", "lights" : [ { "name" : "Overhead", "code" : "1" } ], "room" : "Kitchen" }

Where the last line is of particular interest in illustrating what I want to do. Each document is a room and may have a "lights" key corresponding to a value that is an array of sub-documents. From a modeling perspective, I have a house, which has 0-n rooms, each of which has 0-n lights in it. What I want to do in Java is take the name of the room as a parameter, and return a collection of DBObject corresponding to the sub-documents in the lights array -- "get me all lights for room 'kitchen'", for example.

So far, proceeding incrementally in TDD style, I've constructed this query:

public static final String ROOM_KEY = "room";

public static final String EQUALS_KEY = "$eq";

private BasicDBObject buildRoomNameQuery(String roomName) {

    BasicDBObject myQuery = new BasicDBObject();
    myQuery.put(ROOM_KEY, new BasicDBObject(EQUALS_KEY, roomName));

    return myQuery;
}

I realize that this is going to get me the entire room document for the room name I pass in. I'm a bit stuck on what the best way to proceed from here is to get what I want. Is what I'm doing even possible with a simple query, or will I have to retrieve the array and iterate through it in code, casting the elements as DBObject? I'm also open to suggestions for a better document structure for my purpose -- I'm not married to this structure by any means.

For a bit of perspective, I'm quite well versed in SQL and traditional relational databases, if that helps in terms of explanatory analogies. Also, if I'm butchering the MongoDB terminology, please correct me. Thanks in advance.

like image 436
Erik Dietrich Avatar asked Apr 27 '12 06:04

Erik Dietrich


Video Answer


2 Answers

So, you can do something like this:

DBCollection coll = db.getCollection("test");
BasicDBObject query = new BasicDBObject("room", "Kitchen"); 

// optional, limit the fields to only have the lights field
BasicDBObject fields = new BasicDBObject("lights",1).append("_id",false);
DBCursor curs = coll.find(query, fields);
while(curs.hasNext()) {
  DBObject o = curs.next();

  // shows the whole result document
  System.out.println(o.toString());
  BasicDBList lights = (BasicDBList) o.get("lights");

  // shows the lights array -- this is actually a collection of DBObjects
  System.out.println(lights.toString());

  // optional: break it into a native java array
  BasicDBObject[] lightArr = lights.toArray(new BasicDBObject[0]);
  for(BasicDBObject dbObj : lightArr) {
    // shows each item from the lights array
    System.out.println(dbObj);
  }
}

Also, I recommend using the QueryBuilder in the Java driver--it's a bit more concise than creating Queries from DBObjects. Even better, check out Morphia, which is an object mapper that uses the Java driver. It natively supports entity models that have lists in them, and serializes/deserializes them to Mongo without needing to deal with the DBObject stuff.

like image 175
Eve Freeman Avatar answered Oct 16 '22 05:10

Eve Freeman


Look at spring mongo package. A really good way to work with mongo using POJO documents

http://www.springsource.org/spring-data/mongodb

You will not need to perform casting and work with strings

like image 32
Julias Avatar answered Oct 16 '22 04:10

Julias