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"
}
]
}