This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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
Author

how to select key value of mongodb document?

Joseph Hwang
Greenhorn

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) {
e.getStackTrace();
}

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()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}

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


Thanks,
Tushar (SCJP 1.5)
Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 101
    
  10
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
    
    1
Solution :

 
wood burning stoves
 
subject: how to select key value of mongodb document?
 
Similar Threads
Map Reducer with MongoDB hadoop driver throws Exception!
how to get record from MongoDB
GUI components
How to use regular expressions
update query in mongodb?