KeeperOfTheSevenKeys
KeeperOfTheSevenKeys

Reputation: 69

MongoDB: extra object created when updating with $addToSet

I'm encountering a strange behavior in MongoDB.

This is the schema of the documents in my collection:

{
    "_id" : ObjectId("62bf10951fecaed4dba275b1"),
    "name" : "Rack 1",
    "type" : "rack",
    "positions" : [ 
        {
            "number" : 1
        }, 
        {
            "number" : 2
        }, 
        {
            "number" : 3,
            "nodes" : [ 
                {
                    "number" : 1
                }
            ]
        }, 
        {
            "number" : 4,
            "nodes" : [ 
                {
                    "number" : 1
                }, 
                {
                    "number" : 2
                }, 
                {
                    "number" : 3
                }
            ]
        },
    ]
}

I would like to run two queries:

I have created two queries so far:

db.getCollection('locations').updateOne(
  { _id: ObjectId("62bf10951fecaed4dba275b1") },
  { $addToSet: { 'positions': { number: 5 } } }
)
db.getCollection('locations').updateOne(
  { _id: ObjectId("62bf10951fecaed4dba275b1"), 'positions.number': 5 },
  { $addToSet: { 'positions.$.nodes': { number: 1 } } }
)

The second query creates a new element in nodes. BUT it also creates a new element in the positions array.

This is the erroneous object:

{
    "_id" : ObjectId("62bf10951fecaed4dba275b1"),
    "name" : "Rack 1",
    "type" : "rack",
    "positions" : [ 
        {
            "number" : 1
        }, 
        {
            "number" : 2
        }, 
        {
            "number" : 3,
            "nodes" : [ 
                {
                    "number" : 1
                }
            ]
        }, 
        {
            "number" : 4,
            "nodes" : [ 
                {
                    "number" : 1
                }, 
                {
                    "number" : 2
                }, 
                {
                    "number" : 3
                }
            ]
        },
        {
            "number" : 5,
            "nodes" : [ 
                {
                    "number" : 1
                }
            ]
        },
        {
            "number" : 5     <<<<<< THIS IS WRONG
        },
    ]
}

Is there something I need to know or is simply the wrong query?

Many thanks

Upvotes: 1

Views: 20

Answers (1)

Stephen Taylor
Stephen Taylor

Reputation: 868

The two queries may run at different lengths of time and should not be executed synchronously.

Please try using some async/await style ordering to ensure that the second query does indeed execute when the first query is finished.

e.g. in javascript:

const myFunction = async () => {

  await db.getCollection('locations').updateOne(
    { _id: ObjectId("62bf10951fecaed4dba275b1") },
    { $addToSet: { 'positions': { number: 5 } } }
  )

  await db.getCollection('locations').updateOne(
    { _id: ObjectId("62bf10951fecaed4dba275b1"), 'positions.number': 5 },
    { $addToSet: { 'positions.$.nodes': { number: 1 } } }
  )

}

myFunction()

Upvotes: 0

Related Questions