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" : "[email protected]",
                "_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

1 Answer 1

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}}});
Sign up to request clarification or add additional context in comments.

5 Comments

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" } })
I just saw that. Updated the query.
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
well, you didn't mention that in your question
Sorry, I didn't know that would affect the answer. I'll edit the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.