Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb find not returning results

Tags:

mongodb

Hello I have just started working with mongo and am trying to run a basic find() command.

I have a collection that has records that look like this:

{
"event" : "cheat",
"message" : {
    "tableid" : 205,
    "time" : 1381853433038,
    "link" : "/dbcheat/table205/iID3731"
},
"_id" : ObjectId("525d68f999ddc6a019000004")
}

I would like to find all of the entries that have tableid 205 for example. If I run this command I get back the one row.

db.example.find({
"message" : {
    "tableid" : 205,
    "time" : 1381853433038,
    "link" : "/dbcheat/table205/iID3731"
}
})

But if I just try and run this command I get returned zero results. Does anyone know what could be the issue?

db.example.find({
    "message" : {
        "tableid" : 205
    }
})
like image 747
vcazan Avatar asked Oct 16 '13 14:10

vcazan


2 Answers

You should be able to locate the desired document by doing the following:

db.example.find({ "message.tableid": 205 })
like image 101
thtsigma Avatar answered Sep 18 '22 04:09

thtsigma


The dot notation like in the reply from thtsigma allows you to reach into embedded documents and search. The 2nd way you were trying is saying bring back an embedded document for message that has "only" the following: "tableid" : 205

like image 45
RoganRicheart Avatar answered Sep 18 '22 04:09

RoganRicheart