Sammy Mak
Sammy Mak

Reputation: 11

mongodb cannot update/set document, invalid property id error

I ran into invalid property id error with this query (syntaxError:invalid property id @(shell):3:0):

db.customerOrder.update(
    {
        {"orders.orderNumber":"ord005"},
        {"$set":{"orders.staffNumber":"stf789"}}
    }
)

This is my document:

db.customerOrder.insert(
    {
        "firstName":"Alex",
        "orders":[
                     {"orderNumber":"ord003",
                     "staffNumber":"stf789"},
                     {"orderNumber":"ord005",
                     "staffNumber":"stf890"},
                 ]
    }
)

This query doesnt work too:

db.customerOrder.update(
    {
        {"orders.orderNumber":"ord005"},
        {"$set":{"orders.0.staffNumber":"stf789"}}
    }
)

Am I accessing the orderNumber and staffNumber correctly?

Upvotes: 0

Views: 241

Answers (1)

techstack
techstack

Reputation: 988

I think you are looking for $ operator

db.customerOrder.update(
    {"orders.orderNumber":"ord005"},
    {
        "$set":{"orders.$.staffNumber":"stf789"}
    }
)

Upvotes: 1

Related Questions