This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes MongoDB and the fly likes how to select key value of mongodb document? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » MongoDB
Bookmark "how to select key value of mongodb document?" Watch "how to select key value of mongodb document?" New topic

how to select key value of mongodb document?

Joseph Hwang

Joined: Aug 17, 2013
Posts: 14
My document has two types like below,

{ "_id" : { "$oid" : "529abd9ae4b0bad2656024a4"} , "carrierCode" : "AA" , "carrierName" : "American Airlines Inc." , "delayTime" : { "arrival" : { "date" : "198812" , "times" : 34240}}}
{ "_id" : { "$oid" : "529abd9ae4b0bad2656024a5"} , "carrierCode" : "AA" , "carrierName" : "American Airlines Inc." , "delayTime" : { "departure" : { "date" : "198710" , "times" : 26795}}}

I have to select 'arrival' key or 'departure' key. This is my codes,

Mongo conn = null;

try {
conn = new Mongo("localhost", 27017);
catch(Exception e) {

DB db = conn.getDB("airCarrier");

DBCollection collection = db.getCollection("timeDelay");

BasicDBObject query = new BasicDBObject("carrierCode", "AA");

query.append("delayTime", "departure"); //I have to choose the value, departure or arrival. But this query is wrong! collection.find returns nothing

DBCursor cursor = collection.find(query);
try {
while(cursor.hasNext()) {
} finally {

What does query statement have to be? I need your advice! Thanks
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

Is it possible to change the document structure to
Then you can use $or operator in query

Tushar (SCJP 1.5)
Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 101
You need something like $exists operator. And note the operators reference on a left side of the screen. There are more operators which can be useful in some other scenarios.
Vishal Baid
Ranch Hand

Joined: Jul 18, 2012
Posts: 42
Solution :

I agree. Here's the link:
subject: how to select key value of mongodb document?
Similar Threads
GUI components
How to use regular expressions
update query in mongodb?
how to get record from MongoDB
Map Reducer with MongoDB hadoop driver throws Exception!