Home > Software engineering >  How do I search for the value "city"?
How do I search for the value "city"?

Time:08-11

I'm trying to query through a somewhat simple collection with the following structure:

{
   "name":[
      {
         "something":"",
         "somethingelse":[
            {
               "name":"John",
               "city":"NY"
            }
]}]}

I have tried to search the value "city" with the dot notation but no success.

CodePudding user response:

You can call the data and store it in an object say ob

ob= {
   "name":[
      {
         "something":"",
         "somethingelse":[
            {
               "name":"John",
               "city":"NY"
            }
]}]}

now you can do ob["name"][0]["somethingelse"][0]["city"]

CodePudding user response:

by reading this mongoDB doc, I see that to access array nested document you need to specify the index of the element instead of using ".".

For example with this object:

{
     "name":"bob",
     "info":[
         {"birth":"10/01/1986"},
         {"eyeColor":"blue"},
         {"salary":2000}
     ] 
}

I want to access the "birth" property, I will do something like this:

db.inventory.find({'info.0':"birth"})

Hope this help.

  • Related