Java – retrieves the subdocuments in the array as dbobjects
I'm new to mongodb. I'm using a Java driver I have this file 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" }
The last line is particularly meaningful for explaining what I want to do Each file is a room, and there may be a "light" key corresponding to the value of a sub document array From the perspective of modeling, I have a house with 0-n rooms and 0-n lights in each room What I want to do in Java is to take the name of the room as a parameter and return the dbobject set corresponding to the sub documents in the light column - "give me lights for all rooms" kitchen ", for example
So far, I've done it step by step in the TDD style. I 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 realized that this would let me get the whole room file of the room name I passed I'm a little persistent. The best way to start here is to get what I want What can I even do with a simple query, or do I have to retrieve the array and iterate it in the code to convert the element to dbobject? I can also suggest a better document structure for my purposes - I don't marry this structure in any way
For some points, I am very proficient in SQL and traditional relational databases if it helps to explain the analogy In addition, if I'm killing mongodb terms, please correct me Thank you in advance
Solution
So you can do 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); } }
In addition, I recommend using querybuilder in the Java driver - more concise than creating queries from dbobjects Better yet, check out morphia, an object mapper that uses Java drivers It supports entity models with lists and serializes / deserializes them into Mongo without dealing with dbobjects