0

I have quite a complex data structure in mongoDB. The document looks like a little like this

{ 
    "id" : 0, 
    "basket" : [
        {
            "price" : 0.9918, 
            "id" : 2500, 
            "exGroup" : [
                {
                    "exgId"  : 0, 
                    "ePrice" : 0.9918
                }
                {
                    "exgId"  : 1, 
                    "ePrice" : 0.9918
                }
            ]
        }, ]}

I would like to add an array of documents into the exGroup array of embedded documents so that each of the embedded documents looks like this

                {
                     "id"  : 0, 
                     "price" : 0.9918
                     "order"  : {
                          "id":0,
                          "exec":
                              [{
                                "quantity" : 1,
                                "price"    : 1.0
                                },
                                {
                                "quantity" : 1,
                                "price"    : 1.01
                                }
                              ]
                 }

I have tried to do this with an update query that looks like this:

db.fund.update(
    {
        "id": 0,
        "basket.id": 2500,
        "basket.exGroup.exgId": 0,
        "basket.exGroup.order": {"$exists" : false}
    },
    {
        "$set":
        {
            "basket.exGroup.$.order" : 
             {
               "id":0,
               "exec":
                  [{
                    "quantity":1,
                    "price":0.9978
                  }]
             }
        }
    }
)

Unfortunately this gives me the error "fundId": 0, "date": ISODate("2016-11-21T11:00:00.000+0000"), "basket.assetId": 2500, "basket.exGroup.exgId": 0

Fundamentally my problem is that I don't know how to correctly address a document that is at the leaf of a multi level array (i.e. doc.array.doc.array.doc)

Richard B
  • 895
  • 13
  • 39

1 Answers1

0
db.rich.update(
    {
        "id": 0,
        "basket.id": 2500,
        "basket.exGroup.exgId": 0,
        "basket.exGroup.order": {"$exists" : false}
    },
    {
        "$push":
        {
            "basket.$.exGroup" : 
             {
               "id":0,
               "exec":
                  [{
                    "quantity":1,
                    "price":0.9978
                  }]
             }
        }
    }
)

this will basically update the exGroup and push a new document. however you need model your document accordingly.

satish chennupati
  • 2,602
  • 1
  • 18
  • 27
  • That is very helpful as it helps me make the insert of the document. I'm still not able to understand how I would update one of the values (say price) if I needed to later on. Are you able to help on that? – Richard B Nov 22 '16 at 15:14
  • sure. i will post that too in a while. in middle of something right now. So basically you want to match and update the procie inside basket...exgroup. right ? – satish chennupati Nov 22 '16 at 15:46
  • Yes. If you could edit your comment and show that too I think I'd be good to go. Thanks – Richard B Nov 22 '16 at 16:43