0

I need to update a document by inserting a bid into the Bid array which is part of Bid. An example document is below:

{
  "_id" : "1044300051",
  "Bids" : {
    "Bid" : [
        {
            "Bidder" : {
                "_id" : "pickford25",
                "_Rating" : 255,
                "Location" : "ANIME PARADISE",
                "Country" : "USA"
            },
            "Time" : "Dec-07-01 15:02:54",
            "Amount" : 1.12
        },
        {
            "Bidder" : {
                "_id" : "arlnwtkwsk@aol.com",
                "_Rating" : 61,
                "Location" : "ARCADE, NEW YORK",
                "Country" : "USA"
            },
            "Time" : "Dec-09-01 15:02:54",
            "Amount" : 1.25
        }
    ]
  }
}

Here are some of the queries I have attempted:

db.items.update({_id: "1678348584"}, {$set: {Bids: "Bid[]"}},{$push: {"Bids.Bid": {"Amount":1000}}});
db.items.update({_id: "1678348584"}, {$push: {"Bids": [{"Amount":1000}]}});

The specific _id I am trying to update in the above examples has "Bids" set to null initially

Roc47HS
  • 71
  • 1
  • 12

1 Answers1

1

You are almost there. To access a nested array you'll have to use Bids.Bid. Your query should be something like

db.items.update({_id: "1678348584"}, {$push: {"Bids.Bid": {"Amount":1000}}});
Muhammad Usman
  • 10,039
  • 22
  • 39
  • That gives me this error: WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: Bids.$.Bid" } }) – Roc47HS Apr 19 '18 at 19:56
  • 1
    I just saw that. Updated the query. – Muhammad Usman Apr 19 '18 at 19:57
  • That gives this error: "code" : 16837, "errmsg" : "cannot use the part (Bids of Bids.Bid) to traverse the element ({Bids: null})" —The specific ID I am trying to update has Bids set to null initially – Roc47HS Apr 19 '18 at 19:58
  • well, you didn't mention that in your question – Muhammad Usman Apr 19 '18 at 20:01
  • Sorry, I didn't know that would affect the answer. I'll edit the question. – Roc47HS Apr 19 '18 at 20:05