• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to select key value of mongodb document?

 
Joseph Hwang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to change the document structure to
?
Then you can use $or operator in query
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solution :

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic