[SERVER-47935] findOneAndUpdate and update insert objects instead of arrays despite using array indexes in paths Created: 25/Nov/19  Updated: 05/May/20  Resolved: 05/May/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: R D Assignee: Carl Champain (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-27442 Positional path component in match la... Backlog
is related to SERVER-24748 Aggregation does not provide a way to... Backlog
Operating System: ALL
Participants:

 Description   

 

this.service.db.collection(this.config.mongodb.collections.campaigns)    
.findOneAndUpdate({ 'metadata.campaign_id''123' }, { $set: 
        {
            'status.type.0.documentOne': {
                in_sync: true            
             },
             'status.type.0.documentTwo': {
                in_sync: false   
             }
         }
     }
);

The above code will create `status.type` as an `Object`, whereas this command when run in a shell:

db.getCollection('campaigns').findOneAndUpdate({ 'metadata.campaign_id''123' },
    $set: {    'status.type.0.documentOne': {        in_sync: true    },
    'status.type.0.documentTwo': {        in_sync: false    }})

Will create `status.type` as an Array.

 

db.isMaster() results: 

 

{
    "ismaster" : true,
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 100000,
    "localTime" : ISODate("2019-11-25T22:15:38.241Z"),
    "logicalSessionTimeoutMinutes" : 30,
    "connectionId" : 405472,
    "minWireVersion" : 0,
    "maxWireVersion" : 8,
    "readOnly" : false,
    "ok" : 1.0
}

 

 
 
 



 Comments   
Comment by Carl Champain (Inactive) [ 05/May/20 ]

Hi rd1982rd@gmail.com,

Thank you for the report.
MongoDB doesn't have yet a consistent stance on what "something.0" means, this is why your query creates an object instead of an array. I linked SERVER-27442 and SERVER-24748 which provide more details about this behavior. Please watch them for updates.

However, this should accomplish what you are looking for:

db.campaigns.findOneAndUpdate({b: 1}, 
    [{$set: 
        {"status.type": 
            {$concatArrays: [   
                [{documentOne: {in_sync: true}}],
                [{documentTwo: {in_sync: true}}]
            ]}
        }}]
    )

I will now close this ticket since this is a behavior that we are already aware of. If you need further assistance troubleshooting, I encourage you to ask our community by posting on the MongoDB Community Forums or on Stack Overflow with the mongodb tag.

Kind regards,
Carl

Comment by R D [ 26/Nov/19 ]

May actually have been wrong, running the command in the shell seems to exhibit the same behaviour, so it may be a problem with mongodb itself.

Generated at Thu Feb 08 05:15:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.