[SERVER-57436] mongoDB insert unwanted value in a first order array entry when I insertOne, updateOne or save Created: 04/Jun/21  Updated: 10/Jun/21  Resolved: 09/Jun/21

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

Type: Bug Priority: Major - P3
Reporter: Network Platform Assignee: Edwin Zhou
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

What do I use :

  • MongoDb Atlas
  • NodeJs Drider 3.6.9

Collection params :

  • _id index
  • JsonSchema :

{
  $jsonSchema: {
    bsonType: 'object',
    required: [
      'property_name'
    ],
    properties: {
      property_name: {
        bsonType: 'string'
      },
      keywords: {
        bsonType: 'array'
      },
      description: {
        bsonType: 'object',
        required: [
          'en',
          'fr'
        ],
        properties: {
          en: {
            bsonType: 'string'
          },
          fr: {
            bsonType: 'string'
          }
        }
      },
      alternative_meta: {
        bsonType: 'object',
        properties: {
          meta_description: {
            bsonType: 'string'
          },
          last_scrapp_date: {
            type: [
              'number'
            ]
          },
          last_data_validation: {
            type: [
              'number'
            ]
          }
        }
      },
      product_details: {
        bsonType: 'object',
        properties: {
          mark: {
            bsonType: 'string'
          },
          name: {
            bsonType: 'string'
          },
          license: {
            bsonType: 'string',
            'enum': [
              'proprietary',
              'opensource'
            ]
          },
          distribution: {
            bsonType: 'array'
          },
          type: {
            bsonType: 'string',
            'enum': [
              'saas',
              'software',
              'toolkit',
              'library',
              'code',
              'api'
            ]
          },
          type_history: {
            bsonType: 'array'
          }
        }
      },
      links: {
        bsonType: 'object',
        required: [
          'url',
          'icon'
        ],
        properties: {
          icon: {
            bsonType: 'string'
          },
          url: {
            bsonType: 'string'
          },
          download: {
            bsonType: 'string'
          },
          login: {
            bsonType: 'string'
          }
        }
      },
      pricing: {
        bsonType: 'object',
        properties: {
          price_url: {
            bsonType: 'string'
          },
          price: {
            bsonType: 'string',
            'enum': [
              'fee',
              'free',
              'freemium'
            ]
          },
          price_from: {
            type: [
              'string',
              'null'
            ]
          },
          price_detail: {
            bsonType: 'array'
          }
        }
      },
      opencorpdata: {
        bsonType: 'object'
      },
      language: {
        bsonType: 'array'
      },
      service_locations: {
        bsonType: 'array'
      },
      wikipedia: {
        bsonType: 'object'
      },
      youtube: {
        bsonType: 'object'
      },
      twitter: {
        bsonType: 'object'
      },
      instagram: {
        bsonType: 'object'
      },
      purpose: {
        bsonType: 'array'
      },
      integration: {
        bsonType: 'array'
      }
    }
  }
}

Sequence :

  1. API call to delete a document in the collection
  2. API call to instert a new document in the collection

Driver code :

  1. In the API call

 

Participants:

 Description   

When I insert a document in a collection on mongoDB Atlas via the nodeJs driver some array gets new entry from the previouly delete. Sometime it is null or a duplicate. It would not happen on all the entry of array type. This concerne as far as I know text items and object items.

 



 Comments   
Comment by Network Platform [ 10/Jun/21 ]

Hi Edwin,

I find a way to make it worked. I restructured my json. Now those first order array are second order. In that case, arrays are not modified.

I check every method and data, it does not come from my code.

Regards,

Mathieu

Comment by Edwin Zhou [ 09/Jun/21 ]

Hi projetcreasup@gmail.com,

Thanks for your report. Please note that the SERVER project is for bugs and feature suggestions for the MongoDB server. As this ticket does not appear to be a bug, I will now close it. If you need further assistance troubleshooting, I encourage you to ask our community by posting on the MongoDB Developer Community Forums.

Best,
Edwin

Comment by Network Platform [ 04/Jun/21 ]

As we can't edit the ticket and I miss fire the validation when I was typing. I will follow my explanation in this comment :

I double-checked and the doc object is not malformed and does not contain unwanted values.

First API call :
await col.deleteOne({
property_name: operation.operation.name,
creator_id: req.authUser.id
});
Directly after I procede to the seconde API call :
await
col.updateOne(
{
property_name: doc.property_name,
creator_id: req.authUser.id
},
{
$set: doc
},
{ upsert: true }
);
This result in this :

From "language": ["EN", "FR"], to "language": ["EN", "FR", null, null],

and

From "purpose": [\{"name": "stem and leaf plot","coef": 1}], to "purpose": [\{"name": "stem and leaf plot","coef": 1}, \{"name": "stem and leaf plot","weight": 1}],

From "integration": ["microsoft excel"] to "integration": ["microsoft excel", "microsoft excel"]


 I have tried many methods such as :

  • insertOne
  • insertMany
  • updateOne with $set and $addToSet
  • save

In each case I obtain duplicate item for these three array of my document.

I came to the conclusion that it was an over writing problem. I try with a different document but the same property_name and even if the language array was not in the command MongoDB creates an entry for it. This was the worst case.

 

 

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