skip to Main Content

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.

2

Answers


  1. 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"]

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search