rstewart8
rstewart8

Reputation: 99

ruby mongo db find method

I am trying to retrieve a record from mongodb using this:

client = MongoClient.new("something.com",27017).db("big_data_api_development")
coll = client.collection("dni_logs")
dni = coll.find({"data" => {"session_id" => "aaa"}}).to_a

This is what the record looks like:

{
"_id" : ObjectId("5272198be77989f5be019323"),
"data" : {
    "dni_id" : "102329",
    "phone_number_id" : "10403",
    "phone_number_pool_id" : null,
    "master_node_id" : "686",
    "organizational_unit_id" : "896",
    "phone_number" : "888888888",
    "session_id" : "aaa",
    "ip_host" : "10.204.201.245",
    "browser" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101    Firefox/19.0 AlexaToolbar/alxf-2.17",
    "log_date" : "2013-04-08 19:02:03",
    "referring" : "google",
    "referring_type" : "Organic",
    "search_words" : "Not Available",
    "ref_params" : null,
    "custome_params" : null
},
"created_at" : ISODate("2013-04-09T00:02:03.000Z"),
"_keywords" : [ 
    "02", 
    "03", 
    "04", 
],
"updated_at" : ISODate("2013-10-31T08:49:15.227Z")
}

My return back is an empty array. Is this the correct way to retrieve the record? Thanks

Upvotes: 0

Views: 524

Answers (2)

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

You should use the dot notation:

coll.find('data.session_id' => 'aaa')

Upvotes: 3

usha
usha

Reputation: 29369

Try this

dni = coll.find({data: {session_id: "aaa"}}).to_a

Upvotes: -1

Related Questions