"$fieldName" not working for me

Hello Team ,

I am using mongoos js and execute following query,

finishedproducts.updateOne({ sku: 'XXXL-T1-TMBL' }, { '$set': { skuIdealConsumptionAvlIntventory: '$$NOW' } })

My query update same “$$NOW” in my document.
What wrong with me, please give me solution.
Thanks

In updates you have to use https://docs.mongodb.com/manual/reference/operator/update/currentDate/ rather than $$NOW.

And important passage is:

Starting in version 4.2 , update methods can accept an aggregation pipeline. As such, the previous example can be rewritten as the following using the aggregation stage $set and the aggregation variables NOW (for the current datetime) and CLUSTER_TIME (for the current timestamp):

@steevej Thanks for quick reply.
I need a help for my updateMany query, My query is like this

My Document Structure is :
{
   "_id":{
      "$oid":"61b09087c6379653642cc9d6"
   },
   
   "name":"Seven Rocks Mens Tshirt",
   "sku":"XXXL-T1-TMBL",
   "asin":"B07QMCLYYZ",
   "productIdealConsumption":[
      {
         "id":8470,
         "productIdealConsumptionId":663,
         "productId":1212,
         "qty":0.22,
         "product":{
            "id":1212,
            "sku":"6040CP-SJ-TB-FR-TM",
            "name":"Fabric Roll",
            "categoryId":5
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8471,
         "productIdealConsumptionId":663,
         "productId":1028,
         "qty":0.036,
         "product":{
            "id":1028,
            "sku":"6040CP-SJ-TB-FR-BL",
            "name":"Fabric Roll",
            "categoryId":5
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8472,
         "productIdealConsumptionId":663,
         "productId":1342,
         "qty":0.021,
         "product":{
            "id":1342,
            "sku":"CPL-1X1 RB-TB-FR-BL",
            "name":"Rib Fabric Roll",
            "categoryId":14
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
            
      },
      {
         "id":8473,
         "productIdealConsumptionId":663,
         "productId":2907,
         "qty":136.8,
         "product":{
            "id":2907,
            "sku":"GI-MTR-SEW.THD-2PLY-TM",
            "name":"2PLY",
            "categoryId":72
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0       
            
      },
      {
         "id":8474,
         "productIdealConsumptionId":663,
         "productId":2901,
         "qty":3.75,
         "product":{
            "id":2901,
            "sku":"GI-MTR-SEW.THD-2PLY-BL",
            "name":"2PLY",
            "categoryId":72
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8478,
         "productIdealConsumptionId":663,
         "productId":3058,
         "qty":1,
         "product":{
            "id":3058,
            "sku":"QCI-PCS-WASH CARE LBL-30MM*200MTR",
            "name":"WASH CARE LBL-30MM*200MTR",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8479,
         "productIdealConsumptionId":663,
         "productId":3059,
         "qty":1,
         "product":{
            "id":3059,
            "sku":"QCI-PCS-WASH CARE RIBBON-40MM*300MTR",
            "name":"WASH CARE RIBBON-40MM*300MTR",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
         },
      {
         "id":8480,
         "productIdealConsumptionId":663,
         "productId":3046,
         "qty":1,
         "product":{
            "id":3046,
            "sku":"QCI-PCS-EB TAGS",
            "name":"EB TAGS",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
        },
      {
         "id":8481,
         "productIdealConsumptionId":663,
         "productId":3049,
         "qty":1,
         "product":{
            "id":3049,
            "sku":"QCI-PCS-PACKING GATTA- 10*13",
            "name":"PACKING GATTA- 10*13",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
		 },
      {
         "id":8482,
         "productIdealConsumptionId":663,
         "productId":3053,
         "qty":1,
         "product":{
            "id":3053,
            "sku":"QCI-PCS-PLAIN BOPP BAG-11*16-21MIC.",
            "name":"PLAIN BOPP BAG-11*16-21MIC.",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0,
         },
      {
         "id":8483,
         "productIdealConsumptionId":663,
         "productId":3083,
         "qty":0.001,
         "product":{
            "id":3083,
            "sku":"QCI-LTR-BENZENE CHM.",
            "name":"BENZENE CHM.",
            "categoryId":66
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0,
          },
      {
         "id":8484,
         "productIdealConsumptionId":663,
         "productId":2895,
         "qty":0.75,
         "product":{
            "id":2895,
            "sku":"GCI-MTR-BONE DORI (MOTI)",
            "name":"BONE DORI (MOTI)",
            "categoryId":71
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0,
         }
   ],
   "totalOprationTime":38,
   "totalOprations":38,
   "productId":57,
   "skuIdealConsumptionAvlIntventory":1,
   "skuOperationsTimeAvlIntventory":0,
   "skuAvlIntventory":0,
   "__v":0
} 

finishedproducts.updateMany({ sku: { '$in': [ 'XS-T1-WMBL', 'S-T1-WMBL', 'M-T1-WMBL', 'L-T1-WMBL', 'XL-T1-WMBL', 'XXL-T1-WMBL', 'XXXL-T1-WMBL', 'XS-T1-NMBL', 'S-T1-NMBL', 'L-T1-NMBL', 'XL-T1-NMBL', 'XXL-T1-NMBL', 'XXXL-T1-NMBL', 'XS-T1-KMBL', 'S-T1-KMBL', 'M-T1-KMBL', 'L-T1-KMBL', 'XL-T1-KMBL', 'XXL-T1-KMBL', 'XXXL-T1-KMBL', 'XS-T1-BLAG', 'S-T1-BLAG', 'M-T1-BLAG', 'L-T1-BLAG', 'XL-T1-BLAG', 'XXL-T1-BLAG', 'XXXL-T1-BLAG', 'XS-T1-TMBL', 'S-T1-TMBL', 'M-T1-TMBL', 'L-T1-TMBL', 'XL-T1-TMBL', 'XXL-T1-TMBL', 'XXXL-T1-TMBL' ] }, 'productIdealConsumption.$.productId': 2895}, [ { '$set': { 'productIdealConsumption.$.reservedInventory': '$productIdealConsumption.$.qty' } }], {})

I want to update all subdocument “productIdealConsumption” which productId is “2895” with given document “skus”.
My query given me error , what wrong with my query.
Please look once my query, and suggest me a solution for this.
Thanks.

If my previous post solved the issue of your first post, please mark my post as the solution so that others know they can follow the recommendation.

Post your new question in a new thread.

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