What reasons could there be for updateOne to not modify documents?

I’m trying to figure out why updateOne returns a match but modifies 0 documents.
This is the command I’m running:

db.products.updateOne({
...   '$and': [
...     { seqId: '1' },
...     { category: 1 },
...     { 'variants.attributes.size.length': 24 },
...     { 'variants.attributes.size.width': 36 },
...     { 'variants.attributes.size.uom': 'ft' },
...     { 'variants.attributes.color': 'Clear' }
...   ]
... }
... ,
... { '$set': { 'variants.$.stockStatus': 'In Stock' } });

And this is what the command returns:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}

I’ve confirmed that stockStatus in the existing document is not “In Stock” to rule out that it’s not modifying because the new value is the same.

Are there any other reasons why updateOne would not modify certain documents?

EDIT: variants is an array within a document. In this particular document, there are 4 elements in variants. The issue doesn’t occur if I run the command with the first 4 elements and they are modified correctly. But if I run the command with the last 4 elements, they are not modified, with the return message above.

Please share the document that you expect to be modified.

Please share all alternatives queries you tried. Working off your description is difficult as we might use different terminology.

I just learned that the issue is not that there are no modifications made at all, but that it actually attempted to modify the first element of the array instead of the expected element based on the positional operator.

This is the full document I’m working with:

{
  "_id": {
    "$oid": "63be7ec836bbf46d2cb38e91"
  },
  "category": 1,
  "seqId": "1",
  "brand": "",
  "createdAt": {
    "$date": {
      "$numberLong": "1673428676263"
    }
  },
  "description": "1/16 C",
  "info": "250 or 304/crate",
  "name": "1/16 Float Clear",
  "stockNum": "116CF",
  "tags": [
    "1/16",
    "C",
    "Float",
    "Clear"
  ],
  "uom": "sqft",
  "updatedAt": {
    "$date": {
      "$numberLong": "1673428997773"
    }
  },
  "variants": [
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f86762"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f86763"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86764"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "36"
          },
          "width": {
            "$numberDecimal": "48"
          },
          "type": "standard"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f8675e"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f8675f"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86760"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "12"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f8675a"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f8675b"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f8675c"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "24"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f86756"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f86757"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86758"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "36"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f86752"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f86753"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86754"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "48"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    }
  ]
}

When I run this command, I expect variants.4.stockStatus to be updated, but it ends up updating variants.0.stockStatus:

db.products.update({
...   '$and': [
...     { seqId: '1' },
...     { category: 1 },
...     { 'variants.attributes.size.length': 12 },
...     { 'variants.attributes.size.width': 48 },
...     { 'variants.attributes.size.uom': 'ft' },
...     { 'variants.attributes.color': 'Clear' }
...   ]
... },
... { '$set': { 'variants.$.stockStatus': 'In Stock' } }
... );

On the other hand, if I run this command expecting variants.1.stockStatus to be updated, it behaves correctly.

db.products.update({
...   '$and': [
...     { seqId: '1' },
...     { category: 1 },
...     { 'variants.attributes.size.length': 12 },
...     { 'variants.attributes.size.width': 12 },
...     { 'variants.attributes.size.uom': 'ft' },
...     { 'variants.attributes.color': 'Clear' }
...   ]
... },
... { '$set': { 'variants.$.stockStatus': 'In Stock' } });

Here’s the modified document after running both commands above.
Expected outcome: Variants[4] and Variants[1] should be “In Stock”
Actual outcome: Variants[0] and Variants[1] are “In Stock”, while variants[4] remains to be “Out of Stock”

{
  "_id": {
    "$oid": "63be7ec836bbf46d2cb38e91"
  },
  "category": 1,
  "seqId": "1",
  "brand": "",
  "createdAt": {
    "$date": {
      "$numberLong": "1673428676263"
    }
  },
  "description": "1/16 C",
  "info": "250 or 304/crate",
  "name": "1/16 Float Clear",
  "stockNum": "116CF",
  "tags": [
    "1/16",
    "C",
    "Float",
    "Clear"
  ],
  "uom": "sqft",
  "updatedAt": {
    "$date": {
      "$numberLong": "1673428997773"
    }
  },
  "variants": [
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f86762"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f86763"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86764"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "36"
          },
          "width": {
            "$numberDecimal": "48"
          },
          "type": "standard"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "In Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f8675e"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f8675f"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86760"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "12"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "In Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f8675a"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f8675b"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f8675c"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "24"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f86756"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f86757"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86758"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "36"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    },
    {
      "_id": {
        "$oid": "63be7ec4f871bd6473f86752"
      },
      "attributes": {
        "_id": {
          "$oid": "63be7ec4f871bd6473f86753"
        },
        "size": {
          "_id": {
            "$oid": "63be7ec4f871bd6473f86754"
          },
          "thickness": "1/16",
          "uom": "ft",
          "length": {
            "$numberDecimal": "12"
          },
          "width": {
            "$numberDecimal": "48"
          },
          "type": "cut"
        },
        "color": "Clear",
        "section": "",
        "type": "0"
      },
      "stockStatus": "Out of Stock"
    }
  ]
}

I think that the major issue is that you are not using $elemMatch in your query so $ in variants.$.stockStatus will match whatever element matches

or

but not necessarily the only element for which all conditions are true.

Try using the following for the variants part of the query:

{ 'variants.attributes' : { "$elemMatch" : {
   "size.length" : 12 ,
   "size.witdth" : 12 ,
   "size.uom" : "ft" ,
   "color" : "Clear" 
} } }
1 Like

That solved the problem! Thank you so much!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.