[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Update nested sub Document with a specific condition and tags: ['node-js']

[User]
Text:I need a help for my updateMany query,My query is like this,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.
Code: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"
      }
   }
],
{

})

[Assistant]
Text:I gave a quick look at your query.You do not use $ in the query part.  You should replace"productIdealConsumption.$.productId":2895with
"productIdealConsumption.productId":2895See https://docs.mongodb.com/manual/reference/operator/update/positional/ for examples.  You might need to use https://docs.mongodb.com/manual/reference/operator/update/positional-all/ if more than one productIdealConsumption item refers to the same productId within the same top level document. This is not the case in the sample document you supplied but you should be aware.Your problem is the same as Changing value in nested array from a scalar to an arry.What is unresolved is how to get the positional value, productIdealConsumption.$.qty in your case. As mentioned in the other thread $map is worth investigating.Another avenue would be to $unwind, update the unwinded documents, then $group back all documents and use $out or $merge.
Code:"productIdealConsumption.$.productId":2895"productIdealConsumption.productId":2895

[Assistant]
Text:$map is was worth investigating.See my answer at Mongo query assistance update array element with field from object - #5 by steevej
Code:

[Assistant]
Text:@Seekex_youtube, is your issue resolve?  If it is, please mark my post as the solution so that others know that it works.  This will help keep this post useful and efficient.  It is simply courtesy.
Code:

[Assistant]
Text:Need Help…!!!
What If I want to check two conditions. Like I want to update a field only when two given conditions are true. If either of are false, I don’t want to update that field in that particular document. I also have a sub-doc like following
team: [ {field1, field2, field3,…}, {field1, field2, field3,…}, {field1, field2, field3,…}, …]So, what I want to do is to check if field1 and field2 are satisfied or not. If they both are true/satisfied, make the update to field3, otherwise if either of it are false or not satisfied, don’t make any update.
Code:

[Assistant]
Text:Share real sample documents we can cut-n-paste into our system.Share the expected result.Share the exact condition you want.Share what you have tried and explain us how it failed to deliver the expected result.
Code:

[Assistant]
Text:So, without going into other details of my project, I have a collection containing multiple documents. In each document, there is an array named “team” and inside this array, I have objects. Each individual object contain information for a particular player like name, points, _id and isCaptain (which tells that whether this particular player is a captain of this team or not in the form of ‘true’ and ‘false’ i.e isCaptain: true means that this player is captain of this team and isCaptain: false means that this player is not a captain of this team). From the frontend, I am sending two values; id and points. Now, what I am doing is that I’m fetching/reaching out to all the players(objects) whose _id matches with the input ‘_id’ (coming from the frontend). After this, I want to check if this player is the captain of this team or not. If he is, then I want to $set the points of this player as double points by doing points 2. And if that player is not a captain, then I just want to $set points to the input points without making it double.
I was able to do everything but I’m stuck on isCaptain. I want my code to be modelled in such a way that it checks the matching _id and also check that if that player is captain or not. If he is captain than double the points (points 2) otherwise go to the other update operation. I have attached the code snippet plus the Screenshots of my documents.
Let me know if I was able to make it clear enough for you to understand.

Screen Shot 2022-05-21 at 4.23.42 PM1330×967 81.3 KB

Screen Shot 2022-05-21 at 4.19.37 PM860×768 68.6 KB

Screen Shot 2022-05-21 at 4.20.17 PM983×751 79.3 KB

Screen Shot 2022-05-21 at 4.21.18 PM907×762 68.6 KB

Code:

[Assistant]
Text:We cannot cut-n-paste your documents when you publish them as a screenshot.We cannot cut-n-paste your code when you publish it as a screenshot.We need text thatwe can cut-n-paste into our system.Please read Formatting code and log snippets in posts.
Code:

[Assistant]
Text:Documents are following:Following is my code snippetLet me know if this is fine or if you want me to share the code and documents in other way.
Code:{"_id":{"$oid":"61fb77d0b3185a7a67f34c64"},"team":[{"name":"Abid Ali","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f3"},"isCaptain":false},{"name":"Abdullah Shafique","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f4"},"isCaptain":false},{"name":"Cameron Green","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94616"},"isCaptain":false},{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Glenn Maxwell","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94618"},"isCaptain":false},{"name":"Josh Hazlewood","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94619"},"isCaptain":false},{"name":"Jhye Richardson","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461a"},"isCaptain":false},{"name":"Matthew Wade","role":"wicketkeeper","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461b"},"isCaptain":false},{"name":"Mitchel Starc","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461f"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":true}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7799b3185a7a67f34bee"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"1"}}
{"_id":{"$oid":"61fb7e10b3185a7a67f34d9d"},"team":[{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Usman Khawaja","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94628"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Steven Smith","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94626"},"isCaptain":false},{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":true},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":false},{"name":"Mohammad Rizwan","role":"wicketkeeper","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94606"},"isCaptain":false},{"name":"Hasan Ali","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fe"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false},{"name":"Mitchel Starc","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461f"},"isCaptain":false},{"name":"Shaheen Afridi","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9460c"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7d86b3185a7a67f34d27"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"1"}}
{"_id":{"$oid":"61fc2b8cb3185a7a67f3570f"},"team":[{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Usman Khawaja","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94628"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Steven Smith","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94626"},"isCaptain":false},{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":true},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":false},{"name":"Mohammad Rizwan","role":"wicketkeeper","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94606"},"isCaptain":false},{"name":"Hasan Ali","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fe"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false},{"name":"Mitchel Starc","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461f"},"isCaptain":false},{"name":"Shaheen Afridi","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9460c"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7d86b3185a7a67f34d27"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"1"}}
{"_id":{"$oid":"61ff4b38b3185a7a67f36712"},"team":[{"name":"Abid Ali","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f3"},"isCaptain":false},{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":true},{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Mohammad Rizwan","role":"wicketkeeper","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94606"},"isCaptain":false},{"name":"Shaheen Afridi","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9460c"},"isCaptain":false},{"name":"Hasan Ali","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fe"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false},{"name":"Nathan Lyon","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94623"},"isCaptain":false},{"name":"Steven Smith","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94626"},"isCaptain":false},{"name":"Usman Khawaja","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94628"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61ff4a84b3185a7a67f3669c"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"0"}}
{"_id":{"$oid":"621b0135b3185a7a67f3784a"},"team":[{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":false},{"name":"Fakhar Zaman","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f9"},"isCaptain":false},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":false},{"name":"Haider Ali","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fc"},"isCaptain":true},{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Glenn Maxwell","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94618"},"isCaptain":false},{"name":"Matthew Wade","role":"wicketkeeper","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461b"},"isCaptain":false},{"name":"Cameron Green","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94616"},"isCaptain":false},{"name":"Alex Carey","role":"wicketkeeper","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94615"},"isCaptain":false},{"name":"Scott Boland","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94625"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7d86b3185a7a67f34d27"},"matchid":{"$oid":"61fb77fbb3185a7a67f34cde"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"0"}}
    const { id, points } = req.body;

    MyTeam.find({ 'team._id': id }).where('team.isCaptain').equals(true).updateMany(
        {
            $set: {
                'team.$.points': points*2,
                'totalPoints': points*2
            }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
    )

    MyTeam.find({ 'team._id': id }).where('team.isCaptain').equals(false).updateMany(
        {
            $set: {
                'team.$.points': points,
                'totalPoints': points
            }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
    )

[Assistant]
Text:I do not know the following syntax.Is it mongoose or something like that?You are using $set rather than $inc?  If you do the operation for 2 people of the same team, your total point will be equal to the last value, not the sum of the 2 values.  I am pretty sure that $inc is more appropriate.You are doing 2 database access to do your update.  You should always try to avoid doing 2 db access.  In this case, you could use $cond to $inc (or $set) to a different value based on the isCaptain.Something likeI am surprise that you do that simply for "team._id":id, don’t you want to restrict the update to a given matchid?  For example, Hasan Ali is present in 3 documents.  Your query will update the 3 documents where Hasan Ali is listed.
Code:MyTeam.find({ 'team._id': id }).where('team.isCaptain').equals(true)
"totalPoints" : { "$multiply" : [ points , { "$cond" : [ "$isCaptain" , 2 , 1 ] } ] }
"team._id":id

[Assistant]
Text:Yeah, so I used $set instead of $inc just because of testing. I didn’t wanted to manually reset the values back to original (which is “0”) everytime I make the request to the endpoint since that’s quite time consuming.
Secondly, I was trying this (MyTeam.find({ ‘team._id’: id }).where(‘team.isCaptain’).equals(true)) code but I had another line of code before which is belowIt works the same.
Now coming on to the code you shared, I tried it but I got the following error
‘’’
CastError: Cast to Number failed for value “{ ‘$multiply’: [ 10, { ‘$cond’: [Array] } ] }” (type Object) at path “totalPoints”
messageFormat: undefined,
stringValue: "{ '$multiply': [ 10, { '$cond': [Array] } ] }",
kind: ‘Number’,
value: {
‘$multiply’: [ 10, { ‘$cond’: [ ‘$isCaptain’, 2, 1 ] } ]
},
path: ‘totalPoints’,
reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:{
generatedMessage: true,
code: ‘ERR_ASSERTION’,
actual: false,
expected: true,
operator: ‘==’
},
valueType: ‘Object’
}
‘’’
I modified it in the following way but it is giving me a similar sort of errorSo, how can I fix this error? I just want to have the following implementation.
If isCaptain is true, I want ‘team.$.points’ and ‘totalPoints’ to be assigned or increment as points*2. And if isCaptain is false, I want ‘team.$.points’ and ‘totalPoints’ to be assigned or increment as simply points.My overall code snippet looks like the following:
Code:MyTeam.updateMany({ 'team._id': id }, {
 // Rest of the code is same as above
})
"{ '$multiply': [ 10, { '$cond': [Array] } ] }"assert.ok(!isNaN(val))
 'team.$.points': Number({ "$cond": ["$isCaptain", points*2, points] }),
 'totalPoints': Number({ "$cond": ["$isCaptain", points*2, points] })
    MyTeam.updateMany({ 'team._id': id },
    // MyTeam.where('team._id').equals(id).$where('team.isCaptain').equals(false).updateMany(
        {
            $inc: {
                'team.$.points':{ "$multiply" : [ points , { "$cond" : [ "$isCaptain" , 2 , 1 ] } ] },
                'totalPoints': { "$multiply" : [ points , { "$cond" : [ "$isCaptain" , 2 , 1 ] } ] }
            }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
    )


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Delhi-NCR MUG: Introduction to MongoDB Charts and tags: ['charts', 'delhi-mug']

[User]
Text:Delhi-NCR MongoDB User Group is excited to announce a meetup on the coming weekend to introduce you all to MongoDB Charts. MongoDB Charts is the best way to create stunning visualizations for your data. Join us for a demonstration of visualizing MongoDB data using this amazing tool.It will start with a quick no-code demo followed by a collaborative exercise with some amazing swag to win. Not to forget we have some exciting fun games as well.If you are a beginner or have some experience with MongoDB already, there is something for all you of you!Event Type: Online
Link(s):
Location
Video Conferencing URLTo RSVP - Please click on the “✓ Going” link at the top of this event page if you plan to attend. The link should change to a green button if you are Going. You need to be signed in to access the button.Product Engineer, LooppanelFull-Stack JavaScript Developer, passionate about technology and problem-solving. Possess experience in working with web technologies like ReactJS, AngularJS and Angular2, HTML, and CSS, NodeJS, Express, MongoDB, Firebase, and SQL.ThoughtFocus, Lead Database, and Cloud Operations–
Co-Founder, The Coding Culture–
Software Engineer, LinkedInJoin the Delhi-NCR group to stay updated with upcoming meetups and discussions.
Code:Product Engineer, LooppanelThoughtFocus, Lead Database, and Cloud OperationsCo-Founder, The Coding CultureSoftware Engineer, LinkedIn

[Assistant]
Text:Hi,Will that be an online webinar? Will it be recorded? Is it publicly accessible?
Code:

[Assistant]
Text:Hi,It will be an online event.
Code:

[Assistant]
Text:Hey @NeNaD,
As Sanchit mentioned, this would be an online meetup and thus, open for anyone located anywhere to join.Also, this being a user group meetup would be interactive and collaborative in nature, with the focus being introducing and getting everyone attending up to speed on MongoDB Charts.Hope to have you join and be a part of it.
Code:

[Assistant]
Text:Hi @Harshit,Thanks for additional details. Where can we find the link to join? I will definitely interested! Kind regards,
Nenad
Code:

[Assistant]
Text:That’s great to know The link is collapsed under the “Where” section in the description.
Screenshot 2022-03-25 at 10.59.27 AM1282×308 16.4 KB
I will post here as well for easy access: Launch Meeting - Zoom
Code:

[Assistant]
Text:Hey Everyone,
Gentle Reminder, that the event starts in less than 24 hours. Yay!Here’s how you can be better prepared for the demo and exercise the speaker has for you:Please feel free to reply to this thread and ask if you get stuck anywhere. We are looking forward to seeing you all tomorrow at the event
Code:

[Assistant]
Text:The event begins in less than 25 mins!
Join here: Launch Meeting - Zoom
Code:

[Assistant]
Text:Thanks all for kind information
Code:

[Assistant]
Text:Breakout rooms: We will soon be breaking into rooms with a timer at the top.Exercise Details: Inside the breakout rooms you will see questions you to solve by plotting charts.Submission Form: Once done, or once the time is up, make sure you submit to the form. NOTE: Any team member can submit with their email address used for zoom.Completeness (40%) - Correct use of data to answer all important questions.Visualisation (30%) - Creative and effective use of visual analytics to provide relevant and attractive charts and/or graphs that depict the sameStory Telling (30%) - Capability to tell a compelling and engaging story that is logical and criticalWhat are the total number of restaurants in Brooklyn?Amongst a certain set of restaurants, which have the maximum number of outlets?Which are the most popular restaurants by scores?Which restaurants have the maximum cuisines?Compare different cuisine types served by the restaurantsYou need to submit your details, screenshot, and dashboard link to this form.
Code:

[Assistant]
Text:Hey All, Jayesh Choudhary here,
I was in the 3rd place in trivia
below are my contact details, in case you don’t find my account detailsemail - jayeshchoudhary619@gmail.comIt was very fun and got to lean more about MongoDB
Code:

[Assistant]
Text:Hi @Harshit,Is there a recording of a webinar available somewhere?
Code:

[Assistant]
Text:Hey @NeNaD
There is a recording and we will be publishing it soon.
Code:

[Assistant]
Text:Hey Everyone,
Thanks for joining the event last Saturday.Here’s the recording of the event in case you missed it: MUG Delhi-NCR: Introduction to MongoDB Charts | March 26, 2022 - YouTubeSome important updates:We hope to see you in person at the next meetup!
Code:

[Assistant]
Text:We have the results of the Charts Challenge!Congratulations to all the winners!@Ansh_Ganatra @jayesh_choudhary @Anurag_Gupta1@Preeti_Sharma @Sai_Charan3@Coder_Chirag @Gokul_Ks @i_koala @Parundeep_SinghYou will be reached out this week for taking in your details around sending you Swag.
Code:

[Assistant]
Text:yay
thanks for organizing the event y’all!
Code:

[Assistant]
Text:Congratulations to all the winners!! Let’s keep on learning, growing and improving our skills with some fun and games.
For any roadblocks you may face in this journey, your questions are most welcome at  MongoDB Developer Community.
Also, please free to explore our free MongoDB University courses.Happy learning
Tarun
Code:

[Assistant]
Text:Hello
I’d won the Kahoot Trivia and was supposed to be contacted last week
Is there any update on the same?
Code:

[Assistant]
Text:Hey @Ansh_Rathod1,
Sorry for the delay, we were figuring out some operational things. Expect to hear from us in a couple of days.
Code:


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Copy atlas collection to another database and tags: ['data-modeling', 'database-tools', 'backup']

[User]
Text:We need to clone/ copy a collection from one db (Let’s call it main) to another (Let’s call it analytics) every 24 hours.Currently the best idea I’ve come up with is to do a mongodump to an s3 bucket and then use mongo restore to copy it to the analytics db.Is there any built in tool/ db sync, or is there an established best practice for doing this in mongo? All the docs seem to point at mongodump → restore or older versions of those.
Code:

[Assistant]
Text:Take a look at
Code:

[Assistant]
Text:Hello Kai,  to go a bit deeper,  is this other Database in another cluster or is it all within the same cluster?You could use a delayed node as steevej recommended or you could use a scheduled trigger with $out.
Every 24 hours run a $out to the new collection.Furthermore, if the other database is in another cluster, you could use Atlas Data Lake along with “$out to Atlas” and again a realm trigger to schedule it.This tutorial covers that use case, except instead of $out to S3 you would use $out to Atlas.Learn how to set up a continuous copy from MongoDB into an AWS S3 bucket in Parquet.Lastly, we actually have some new functionality coming at MongoDB world that I think might actually solve your problem better than these others,  please reach out at benjamin.flast@mongodb.com if you’d like to discuss it further and I can get you early access.Best,
Ben
Code:

[Assistant]
Text:Looking forward to this:some new functionality coming at MongoDB world that I think might actually solve your problem better than these others
Code:

[User]
Text:Hi @steevej & @Benjamin_Flast, thanks for the responses and links.The other database will be within a seperate cluster; the idea being to create some anonymised data in the collection based on various collections in the main database and the copy this to a seperate db which will be used for analytics through Tableau (Assuming we’ll be using the mongo Tableau connector) without any connection or reference back to the main db.I’ll send you an email, wouldn’t mind having a look at the new functionality if it’s going to be a better solution.Cheers
Kai
Code:

[User]
Text:Hi @Benjamin_Flast thanks again.
Looks like we’ll take the DataLake route using a trigger with $merge or $out.I have one last question around DataLake & Atlas triggers.Is there a way to setup DataLake or Atlas triggers locally or in a Docker container?
We currently have a mongodb-memory-server that gets spun up for our integration tests. I’m hoping to do something similar for this scheduled task that will copy / update the analytics db. My thinking isn’t to test the triggers or mongo functionality; I assume you have that covered.
I mainly want to have a canary test to indicate if something may be broken and ensure our queries are correct so we can catch things early. For example if we have a schema change that may affect the aggregate query or something like that.In case someone else stumbles across this thread searching for the same thing our approach as a rough/ pseudocode-ish overview on the same db using the mflix movies example collection is as follows:
Code:exports = async function() {

  const movies = context.services
    .get("DataLake0")
    .db("Database0")
    .collection("Collection0");

    const pipeline = [
      {
            $match: {}
      }, {
    "$out": {
      "atlas": {
        "projectId": "111111111111111111111111",
        "clusterName": "mflix",
        "db": "analytics",
        "coll": "test"
      }
    }
}
   ];
  return movies.aggregate(pipeline);
};

[Assistant]
Text:Hello Kai,Unfortunately no there is no way to deploy Data Lake or Triggers locally,  they are only available in Atlas.Do you have a Dev or QA project in Atlas where you could run these integration tests on a low tier cluster?-Ben
Code:

[User]
Text:pseudocodeHi Ben,We do, the problem is if we use those the data could potentially change when someone uses those environments leading to inconsistent automated tests.
I’ll just test the query in isolation for now using the in-memory db. Ideally it would be a little closer to production, but at least it will cover the most likely potential cause of issues.Cheers
Kai
Code:


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Advice storing phone number and tags: ['data-modeling']

[User]
Text:Hello,I’m designing a mongodb with a friend for a small project and I was wondering if there is any good habits concerning storing the phone number?
Should I just put a String or divide the phone number into 2 the country code + phone number ?
Code:

[Assistant]
Text:Hi @Yoni_40785,Typically phone numbers are stored as text and in my experience, the storage format depends on a couple of requirements/variables:So if you have answers to these questions you’ll be able to come up with a solution that meets those requirements. If you don’t have all the answers yet, you can begin with creating two fields; first field to store the original input string and the second string that strips out any non numeric characters (i.e. convert the first “+” to 00, remove hyphens, parenthesis, dots etc), and index the second. This way you can drop/refine one column if the requirements are a lot less.In terms of actual schema format, you could use a sub-document of arrays with labels for each phone type (mobile, tel 1, home, work etc). The Attribute Pattern can come in handy here too… depending on the requirements.Some of the other guys (@steevej-1495 and @Ramachandra_37567) may have their own ways of dealing with phone numbers.
Code:

[Assistant]
Text:I have use string and the Attribute Pattern for phone numbers.
Code:

[User]
Text:And what have you done with the country code ?I would be storing 3 phones per userI was thinking of using :Could I use the attribute pattern this way:and then iterate over the name ?
Code:"phones":[
{
 "countryCode": "<String>",
 "phoneNumber": "<String>"
}]
"phones":[
  {"k":"countryCodePhone1", "v": "<String>"},
  {"k":"phoneNumber1", "v":"<String>"}
]

[Assistant]
Text:No special treatment for the country code as the phone numbers were for human to read only.example
Code:


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: The Journey of #100DaysOfCode (@henna_dev) and tags: ['100daysofcode']

[User]
Text:Hello Everyone, I had heard a lot on #100DaysOfCode and really wanted to work together with friends on this.  I would be posting every day about my 100 days of progress in this post and I hope I can convince you to follow along with me… Below I am adding progress from previous days. but will now regularly update from today. If you are game, do join me on this adventure challenge Cheers
Henna | Twitter | LinkedIn
Code:

[User]
Text:100DaysOfCode Log
Reading time: 1 min read
Share on Twitter: https://twitter.com/henna_dev/status/1490818405431185410
Code:

[User]
Text:I could not spend a lot of time on code as it was not one of my best days :-/ so I spent some time learning about UI/UX from the Frontend…
Reading time: 1 min read
Share on Twitter: https://twitter.com/henna_dev/status/1491203210945908736
Code:

[User]
Text:I am following the Frontend Track on Jetbrains Academy and continuing with concepts for the 2nd stage of the Flashcard project.
Reading time: 2 min read
Share on Twitter: https://twitter.com/henna_dev/status/1491550126430691339
Code:

[User]
Text:Today, I learned about Interface Components and what part color plays in the UI design of a web application
Reading time: 1 min read
Share on Twitter: https://twitter.com/henna_dev/status/1491926747113263105
Code:

[User]
Text:In CSS, colors can be defined in several different ways.
Reading time: 3 min read
Share on Twitter: https://twitter.com/henna_dev/status/1492265941694042114
Code:

[User]
Text:Images
Reading time: 3 min read
Share on Twitter: https://twitter.com/henna_dev/status/1492612762849136643
Code:

[User]
Text:Before Flexbox Layout Module, the layout of documents on an HTML page were controlled by position, float, display and clear CSS properties…
Reading time: 6 min read
Share on Twitter: https://twitter.com/henna_dev/status/1493009161621323783
Code:

[User]
Text:Ok, I confess Flexbox has not been an easy ride. The most difficult part has been figuring out the flex-basis, flex-grow, flex-shrink…
Reading time: 3 min read
Share on Twitter: https://twitter.com/henna_dev/status/1493363667252961281
Code:

[User]
Text:I took a break from Flexbox and did some Realm today. I will not say this was easy but I have been trying to understand this topic for a…
Reading time: 3 min read
Share on Twitter: https://twitter.com/henna_dev/status/1493731861771739136
Code:

[User]
Text:Ok, I am back on Flexbox Layout :D I have started to get the hang of it and I am liking it. Only If magically I can get some design skills…
Reading time: 4 min read
Show on Twitter: https://twitter.com/henna_dev/status/1494083921256058881
Code:

[User]
Text:Flexbox Layout was a difficult layout when I was reading about it and it is maintaining consistency, still being a difficult one while I…
Reading time: 1 min read
Show on Twitter: https://twitter.com/henna_dev/status/1494418156810612738?
Code:

[User]
Text:I very much believe in the power of working together, no matter what tech you are learning or want to learn, if you like to work with me…
Reading time: 3 min read
Show on Twitter: https://twitter.com/henna_dev/status/1494822081162321924
Code:

[User]
Text:I am absolutely in love with 100Days and I am so glad I started this with my colleague Kushagra who inspires me every day and we both…
Reading time: 2 min read
Share on Twitter: https://twitter.com/henna_dev/status/1495170877184483328
Code:

[User]
Text:Today I spent time learning Navigation Architecture Library of Jetpack Navigation so that I can implement the same in my BookLog…
Reading time: 3 min read
Share on Twitter: https://twitter.com/henna_dev/status/1495548266922053633
Code:

[User]
Text:It was a tiring day today but I am happy I still made some progress and in turn discovered more errors… Today has been a slow and lazy day. My brain went on leave and I could not finish what I had originally planned. It is OK to be not OK…
Reading time: 2 min read
Share on Twitter: https://twitter.com/henna_dev/status/1495908425141735424Cheers
Code:

[User]
Text:Today was a long day but it was fun. I am pretty much happy and satisfied with how the work is going oh GOSH!!, I was missing development soo much , so glad to be back at it…This app idea may turn into an article once I am finished with it and may write a full-fledged piece rather than broken into days as it is currently Today has been a long day and I realized I put my head into stuff where I can avoid :P Lol…. Although I am absolutely happy with where…
Reading time: 3 min read
Share on Twitter: https://twitter.com/henna_dev/status/1496235660516241416
Code:

[User]
Text:Today was a long day, the second half was full of meetings, something I have started to dislike lately… My head is zooming out in the zoom Today has been a looooooonnnnggggg day. I could not make a lot of progress on the BookLog App as much as I wanted to but I worked on a…
Reading time: 2 min read
Share on Twitter: https://twitter.com/henna_dev/status/1496631546715021315
Code:

[User]
Text:Today was a mix of Flexbox and Realm Sync. I am still struggling to make a 3 * 3 square matrix  I wish I was good at maths  but there is a new Realm Byte today…Share on Twitter: https://twitter.com/henna_dev/status/1496965241724600320
Code:


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: The Journey of #100DaysOfCode (@im_Kushagra) and tags: ['100daysofcode']

[User]
Text:Hello Everyone, I had seen a lot on #100DaysOfCode on various Social platforms and recently got inspired by @henna.s to work together on this. I would be posting every day about my 100 days of progress in this post and I hope I can convince you to follow along with me… Below I am adding progress from previous days. but will now regularly update from today.Cheers
Kushagra | Twitter | LinkedIn
Code:

[User]
Text:Today I Learned: About transitionEnd, Template Literals, Playing audio files in JavaScript, The difference between addEventListener!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3oY5e1x
Code:

[User]
Text:Today I Learned: Setting inline styles using Element.style, Transforming an element by default at the center, & Setting…
Reading time: 3 min read
Share on Twitter: https://bit.ly/3HX8K3T
Code:

[User]
Text:Today I Learned: forEach, addEventListener — change, mousemove, CSS variable and handling it with JS, element.style.setProperty!
Reading time: 1 min read

Share on Twitter: https://bit.ly/3rXDgoK
Code:

[User]
Text:Today I Learned: Arrow functions, map() and sort() will always return the same amount of items, accumulator is just a fancy word for total!
Reading time: 5 min read
Share on Twitter: https://bit.ly/3oVd5Np
Code:

[User]
Text:Today I Learned: Little about Flexbox, toggle() method, Using includes() we can check certain word or character that we want and Logger!
Reading time: 4 min read
Share on Twitter: https://bit.ly/3gUwPMC
Code:

[User]
Text:Today I Learned: fetch() method, regex, & pattern-matching!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3H9jSJP
Code:

[User]
Text:Today I Learned: 4 more array methods, More ES6 style code, & Some cool HTML tag!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3GWZCKY
Code:

[User]
Text:Today I Learned: getContext(), Mother-effing, & HTML Canvas!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3HWe3Ah
Code:

[User]
Text:Today I Learned: Various Dev Tools Tricks!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3BtIJ9Z
Code:

[User]
Text:Today I Learned: Tracking key-presses like shiftKey && checked-boxes & QuerySelectorAll!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3LFM0Yj
Code:

[User]
Text:Today I Learned: Create HTML5 video player and addEventListener() is better vs adding onclick=”myFunc()” to a DOM element!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3rXTWfF
Code:

[User]
Text:Today I Learned: How to Detect sequence of Key pressed, also what is Konami Code. It is nothing but Up, Up, Down, Down, Left, Right, Left..
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3LMiXCk
Code:

[User]
Text:Today I Learned: Debounce function, element.classList.add() & element.classList.remove()✨
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3I6PKzM
Code:

[User]
Text:Today I Learned: String, Number and Boolean, Objects and Arrays in JS, Difference b/w referencing and copying, About Embedded object!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3sT0Xh8
Code:

[User]
Text:Today I learned: About Local Storage of Browser Event Delegation!
Reading time: 3 min read
Shared on Twitter: https://bit.ly/3s6IGNX
Code:

[User]
Text:Today I Learned: About offsetWidth and offsetHeight, Dynamic Text Shadow using Javascript!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/36AifIn
Code:

[User]
Text:Today I learned: Sorting in JS in a really different way, Difference between toString() and join()!
Reading time: 3 min read
Shared on Twitter: https://bit.ly/355l0kn
Code:

[User]
Text:Today I learned: Array.prototype.reduce(), Nodelist vs Array!
Reading time: 2 min read
Shared on Twitter: https://bit.ly/3sekjhv
Code:


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Connect MongoDB Compass (Windows) to WSL2 mongod server and tags: ['compass']

[User]
Text:After being able to start mongod server and mongo shell in WSL2, I wish to use MongoDB Compass installed on Windows to connect with the mongod server in WSL2. However I haven’t been able to do so. I tried to connect with URI “mongodb://localhost:27017” in Compass after starting mongod in WSL2 at port 27017, but the attempt was not successful. This is surprising because WSL2 supports local host forwarding (i.e., 127.0.0.1 on WSL2 is the same 127.0.0.1 on Windows).Any suggestion will be appreciated. Thanks!
Code:

[Assistant]
Text:Check these links.May help
Code:

[User]
Text:Thanks Ramachandra. Unfortunately, all methods in the first link did not work (e.g. I tried wsl --shutdown and disabling fast startup), and the second link answered the opposite of my question (connect to Windows MongoDB from WSL).As a guess, is the issue related to how mongodb protocol is configured? For example, If I run python -m http.server --bind 127.0.0.1 8000 in WSL and visit “http://127.0.0.1:8000” in Firefox on Windows, everything works. I can say that localhost forwarding of WSL works for HTTP protocol. Is it possible that MongoDB network protocol does have the same support?
Code:python -m http.server --bind 127.0.0.1 8000

[User]
Text:After much tweaking, I found a workaround but it may have security risk. I would appreciate if anyone could recognize the risk and recommend a better solution.Step 1: (in WSL2) Start mongod but bind to all IP, not just localhostStep 2: (in WSL2) Find the IP address of WSL2Copy the IP address right after “inet”. For example, if the result returns “inet 162.38.25.44/20 brd 162.38.30.255 scope global eth0”, then the IP address is 162.38.25.44.Step 3: (in Windows) Suppose my WSL2 IP address is 162.38.25.44, then run MongoDB compass and connect to the address “mongodb://162.38.25.44:27017”What I don’t understand is why the first step requires binding to all IP, while binding to just localhost or 127.0.0.1 will not work. Any suggestions?
Code:$ mongod --bind-ip-all
$ ip addr | grep eth0


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Index not used in $lookup and tags: ['aggregation', 'indexes']

[User]
Text:I have the look up code as belowThis stage is not using the index on Category. Is there any way to achieve that?Thanks
Code:{ "from" : "Books",
"let" : {
"code" : "$Code"},
"pipeline" : [{ "$match" :
{ "$expr" : { "$and" : [{ "$eq" :
["$Code", "$$code"] },
{ "$eq" : ["$Category", "LPR"] },
{ "$eq" : ["$MonthProcessed", 202202] }] } } }],
"as" : "Books" }

[Assistant]
Text:Hi @Sangeetha_Vinusabari,Can you share getIndexes from both collection and the execution plan?Please note that the $expr lookups were greatly improved with association to indexes in 5.0+ version. What version you use and will it be possible to upgrade?Thanks
Pavel
Code:

[User]
Text:We are using Mongo db server 4.4.

image888×317 14.9 KB
Explain PlanThere is no possibility in the near future to upgrade to 5.0version
Code:{
   "queryPlanner":{
      "plannerVersion":1,
      "namespace":"DEV-T01-DR.Library",
      "indexFilterSet":false,
      "parsedQuery":{

      },
      "winningPlan":{
         "stage":"COLLSCAN",
         "direction":"forward"
      },
      "rejectedPlans":[

      ]
   },
   "executionStats":{
      "executionSuccess":true,
      "nReturned":1,
      "executionTimeMillis":0,
      "totalKeysExamined":0,
      "totalDocsExamined":1,
      "executionStages":{
         "stage":"COLLSCAN",
         "nReturned":1,
         "executionTimeMillisEstimate":0,
         "works":3,
         "advanced":1,
         "needTime":1,
         "needYield":0,
         "saveState":0,
         "restoreState":0,
         "isEOF":1,
         "direction":"forward",
         "docsExamined":1
      },
      "allPlansExecution":[

      ]
   },
   "serverInfo":{
      "host":"GWT-5CG0356LF0",
      "port":27017,
      "version":"4.2.15",
      "gitVersion":"d7fd78dead621a539c20791a93abec34bb1be385"
   },
   "ok":1
}"."

[Assistant]
Text:@Sangeetha_Vinusabari ,First it looks like there is no data to scan so why would an index be preferred?Second you should create an index on all three equality fields combine to answer the prediction…Having said that the $expr option is not optimized in 4.4 unfortunately… According to the execution plan it actually looks like the server is 4.2.15 …Can you find or change the data model to use localField and foreignField syntax as it will be better for indexing…Pavel
Code:totalKeysExamined”:0

[User]
Text:First it looks like there is no data to scan so why would an index be preferred?
I just created a sample collection to get the data u have requested with 1 document
Code:

[Assistant]
Text:Ok… In order to test performance and index behaviour I suggest to have a data set with a reasonable amount of test data.Its hard to predict any execution outcomes with 1 or 2 documents involved…Thanks
Pavel
Code:

[Assistant]
Text:@Sangeetha_Vinusabari there may be some confusion as to what the explain results are showing.For example, if we try the following in MongoDB 4.2.15:the output would show that the operation is performing a COLLSCAN (as you’ve witnessed as well):This is due to pipelines only being able to use indexes for specific initial stages (ex: $match or $sort).In our sample above, the first stage is a $lookup, which results in a full COLLSCAN as every document in the foo collection must be scanned and passed to the $lookup stage.In the $lookup stage (targeting the bar collection in our example) if an index exists on { Category: 1 } it is actually being used!Though the explain output doesn’t show it directly, we can wrap the operation in a custom function to measure the index usage:The output to the above shows (as we expect) no indexes touched on foo, but 1 index hit on bar:Explain output was improved in MongoDB 5.0 (see SERVER-53762) which makes it easier to determine what indexes were used by the $lookup stage.
Code:db.foo.drop(); db.foo.insert({ Code: 1 });
db.bar.drop(); db.bar.insert({ Code: 1, Category: "LPR", MonthProcessed: 202202 });
db.foo.explain("executionStats").aggregate([
{ $lookup: {
  "from": "bar",
  "let": { "code": "$Code" },
  "pipeline": [{
    "$match": {
      "$expr": {
        "$and": [
          { "$eq": ["$Code", "$$code"] },
          { "$eq": ["$Category", "LPR"] },
          { "$eq": ["$MonthProcessed", 202202] }
        ]
      }
    }
  }],
  "as": "Books"
}}]);
COLLSCAN"executionStats" : {
  "executionSuccess" : true,
  "nReturned" : 1.0,
  "executionTimeMillis" : 1.0,
  "totalKeysExamined" : 0.0,
  "totalDocsExamined" : 1.0,
  "executionStages" : {
      "stage" : "COLLSCAN",
      "nReturned" : 1.0,
      "executionTimeMillisEstimate" : 0.0,
      "works" : 3.0,
      "advanced" : 1.0,
      "needTime" : 1.0,
      "needYield" : 0.0,
      "saveState" : 1.0,
      "restoreState" : 1.0,
      "isEOF" : 1.0,
      "direction" : "forward",
      "docsExamined" : 1.0
  }
}
$match$sort$lookupCOLLSCANfoo$lookup$lookupbar{ Category: 1 }function MEASURE_INDEX_USAGE(block) {
  var statsCmd = [{ $indexStats: {} }];
  // measure index usage
  var statsBefore = {};
  db.getCollectionNames().forEach(function (c) {
    statsBefore[c] = {};
    db.getCollection(c).aggregate(statsCmd).forEach(function (d) {
      statsBefore[c][d.name] = d.accesses.ops * 1.0;
    });
  });

  block();

  // measure index usage again
  var stats = {};
  db.getCollectionNames().forEach(function (c) {
    stats[c] = {};
    db.getCollection(c).aggregate(statsCmd).forEach(function (d) {
      if (!statsBefore[c].hasOwnProperty(d.name)) {
        stats[c][d.name] = d.accesses.ops;
      } else if (statsBefore[c][d.name] != d.accesses.ops) {
        stats[c][d.name] = (d.accesses.ops - statsBefore[c][d.name]) + " (" + d.accesses.ops + " total)";
      }
    });
  });

  printjson(stats);
}
db.bar.createIndex({ Category: 1 });
MEASURE_INDEX_USAGE(function () {
db.foo.aggregate([
{ $lookup: {
  "from": "bar",
  "let": { "code": "$Code" },
  "pipeline": [{
    "$match": {
      "$expr": {
        "$and": [
          { "$eq": ["$Code", "$$code"] },
          { "$eq": ["$Category", "LPR"] },
          { "$eq": ["$MonthProcessed", 202202] }
        ]
      }
    }
  }],
  "as": "Books"
}}]);
});
foobar{
    "bar" : {
        "Category_1" : "1 (1 total)"
    },
    "foo" : {

    }
}
$lookup

[Assistant]
Text:What about $expr in $matchMy Model exampleIndexes of my collectionSituation 1.Result 1Situation 1 working fine butSituation 2.
with $expr scanning all documents in collection
why I need it to be $expr because then I need it to be inside $lookupResult 2
Code:{
    "_id" : ObjectId("622f573cb9fd75a8f988cdb6"),
    "branchId" : ObjectId("6212f2fa0615b313e2eb83f5"),
    "groupId" : ObjectId("622f573cb9fd75a8f988cdb4"),
    "teacherId" : ObjectId("622f4f70475460a853fd8fa1"),
    "date" : ISODate("2022-03-15T00:00:00.000+0000"),
    "state" : "created",
    "createdAt" : ISODate("2022-03-14T14:54:52.850+0000"),
    "updatedAt" : ISODate("2022-03-14T14:54:52.850+0000"),
    "deletedAt" : ISODate("2022-03-16T09:04:15.740+0000")
}
[
    {
        "v" : 2.0,
        "key" : {
            "_id" : 1.0
        },
        "name" : "_id_"
    },
    {
        "v" : 2.0,
        "key" : {
            "date" : 1.0,
            "groupId" : 1.0
        },
        "name" : "UniqueGruopLesson",
        "background" : true,
        "unique" : true,
        "partialFilterExpression" : {
            "deletedAt" : {
                "$eq" : null
            }
        }
    }
]

db = db.getSiblingDB("TEST_DB");
db.getCollection("LESSONS").explain("executionStats")
.aggregate(
    [
        {
            $match: {
                groupId: ObjectId("627f7821c5e9de1b328ea918"),
                date:  ISODate("2022-05-30T00:00:00.000+0000"),
                deletedAt:null
            }
        }
    ]
);
{
    "explainVersion" : "1",
    "queryPlanner" : {
        "namespace" : "TEST_DB.LESSONS",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "date" : {
                        "$eq" : ISODate("2022-05-30T00:00:00.000+0000")
                    }
                },
                {
                    "deletedAt" : {
                        "$eq" : null
                    }
                },
                {
                    "groupId" : {
                        "$eq" : ObjectId("627f7821c5e9de1b328ea918")
                    }
                }
            ]
        },
        "optimizedPipeline" : true,
        "maxIndexedOrSolutionsReached" : false,
        "maxIndexedAndSolutionsReached" : false,
        "maxScansToExplodeReached" : false,
        "winningPlan" : {
            "stage" : "FETCH",
            "filter" : {
                "deletedAt" : {
                    "$eq" : null
                }
            },
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "date" : 1.0,
                    "groupId" : 1.0
                },
                "indexName" : "UniqueGruopLesson",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "date" : [

                    ],
                    "groupId" : [

                    ]
                },
                "isUnique" : true,
                "isSparse" : false,
                "isPartial" : true,
                "indexVersion" : 2.0,
                "direction" : "forward",
                "indexBounds" : {
                    "date" : [
                        "[new Date(1653868800000), new Date(1653868800000)]"
                    ],
                    "groupId" : [
                        "[ObjectId('627f7821c5e9de1b328ea918'), ObjectId('627f7821c5e9de1b328ea918')]"
                    ]
                }
            }
        },
        "rejectedPlans" : [

        ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1.0,
        "executionTimeMillis" : 0.0,
        "totalKeysExamined" : 1.0,
        "totalDocsExamined" : 1.0,
        "executionStages" : {
            "stage" : "FETCH",
            "filter" : {
                "deletedAt" : {
                    "$eq" : null
                }
            },
            "nReturned" : 1.0,
            "executionTimeMillisEstimate" : 0.0,
            "works" : 2.0,
            "advanced" : 1.0,
            "needTime" : 0.0,
            "needYield" : 0.0,
            "saveState" : 0.0,
            "restoreState" : 0.0,
            "isEOF" : 1.0,
            "docsExamined" : 1.0,
            "alreadyHasObj" : 0.0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 1.0,
                "executionTimeMillisEstimate" : 0.0,
                "works" : 2.0,
                "advanced" : 1.0,
                "needTime" : 0.0,
                "needYield" : 0.0,
                "saveState" : 0.0,
                "restoreState" : 0.0,
                "isEOF" : 1.0,
                "keyPattern" : {
                    "date" : 1.0,
                    "groupId" : 1.0
                },
                "indexName" : "UniqueGruopLesson",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "date" : [

                    ],
                    "groupId" : [

                    ]
                },
                "isUnique" : true,
                "isSparse" : false,
                "isPartial" : true,
                "indexVersion" : 2.0,
                "direction" : "forward",
                "indexBounds" : {
                    "date" : [
                        "[new Date(1653868800000), new Date(1653868800000)]"
                    ],
                    "groupId" : [
                        "[ObjectId('627f7821c5e9de1b328ea918'), ObjectId('627f7821c5e9de1b328ea918')]"
                    ]
                },
                "keysExamined" : 1.0,
                "seeks" : 1.0,
                "dupsTested" : 0.0,
                "dupsDropped" : 0.0
            }
        }
    },
    "command" : {
        "aggregate" : "LESSONS",
        "pipeline" : [
            {
                "$match" : {
                    "groupId" : ObjectId("627f7821c5e9de1b328ea918"),
                    "date" : ISODate("2022-05-30T00:00:00.000+0000"),
                    "deletedAt" : null
                }
            }
        ],
        "cursor" : {

        },
        "$db" : "TEST_DB"
    },
    "serverInfo" : {
        "host" : "nitro",
        "port" : 2717.0,
        "version" : "5.0.8",
        "gitVersion" : "c87e1c23421bf79614baf500fda6622bd90f674e"
    },
    "serverParameters" : {
        "internalQueryFacetBufferSizeBytes" : 104857600.0,
        "internalQueryFacetMaxOutputDocSizeBytes" : 104857600.0,
        "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600.0,
        "internalDocumentSourceGroupMaxMemoryBytes" : 104857600.0,
        "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600.0,
        "internalQueryProhibitBlockingMergeOnMongoS" : 0.0,
        "internalQueryMaxAddToSetBytes" : 104857600.0,
        "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600.0
    },
    "ok" : 1.0,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1652524686, 1),
        "signature" : {
            "hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1652524686, 1)
}

$expr$expr$lookupdb = db.getSiblingDB("TEST_DB");
db.getCollection("LESSONS").explain("executionStats")
.aggregate(
    [
        {
            $match: {
                $expr: {
                    $and: [
                        {
                            $eq: [
                                 "$groupId",
                                 ObjectId("627f7821c5e9de1b328ea918"),
                            ]
                        },
                        {
                            "$eq": [
                                "$deletedAt",
                                null
                            ]
                        },
                        {
                            $eq:[
                                "$date",
                                ISODate("2022-05-30T00:00:00.000+0000")
                            ]
                        }
                    ]
                }
            }
        }
    ]
);

{
    "explainVersion" : "1",
    "queryPlanner" : {
        "namespace" : "TEST_DB.LESSONS",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "$expr" : {
                        "$and" : [
                            {
                                "$eq" : [
                                    "$groupId",
                                    {
                                        "$const" : ObjectId("627f7821c5e9de1b328ea918")
                                    }
                                ]
                            },
                            {
                                "$eq" : [
                                    "$deletedAt",
                                    {
                                        "$const" : null
                                    }
                                ]
                            },
                            {
                                "$eq" : [
                                    "$date",
                                    {
                                        "$const" : ISODate("2022-05-30T00:00:00.000+0000")
                                    }
                                ]
                            }
                        ]
                    }
                },
                {
                    "date" : {
                        "$_internalExprEq" : ISODate("2022-05-30T00:00:00.000+0000")
                    }
                },
                {
                    "deletedAt" : {
                        "$_internalExprEq" : null
                    }
                },
                {
                    "groupId" : {
                        "$_internalExprEq" : ObjectId("627f7821c5e9de1b328ea918")
                    }
                }
            ]
        },
        "optimizedPipeline" : true,
        "maxIndexedOrSolutionsReached" : false,
        "maxIndexedAndSolutionsReached" : false,
        "maxScansToExplodeReached" : false,
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : [
                    {
                        "$expr" : {
                            "$and" : [
                                {
                                    "$eq" : [
                                        "$groupId",
                                        {
                                            "$const" : ObjectId("627f7821c5e9de1b328ea918")
                                        }
                                    ]
                                },
                                {
                                    "$eq" : [
                                        "$deletedAt",
                                        {
                                            "$const" : null
                                        }
                                    ]
                                },
                                {
                                    "$eq" : [
                                        "$date",
                                        {
                                            "$const" : ISODate("2022-05-30T00:00:00.000+0000")
                                        }
                                    ]
                                }
                            ]
                        }
                    },
                    {
                        "date" : {
                            "$_internalExprEq" : ISODate("2022-05-30T00:00:00.000+0000")
                        }
                    },
                    {
                        "deletedAt" : {
                            "$_internalExprEq" : null
                        }
                    },
                    {
                        "groupId" : {
                            "$_internalExprEq" : ObjectId("627f7821c5e9de1b328ea918")
                        }
                    }
                ]
            },
            "direction" : "forward"
        },
        "rejectedPlans" : [

        ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1.0,
        "executionTimeMillis" : 25.0,
        "totalKeysExamined" : 0.0,
        "totalDocsExamined" : 28851.0,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : [
                    {
                        "$expr" : {
                            "$and" : [
                                {
                                    "$eq" : [
                                        "$groupId",
                                        {
                                            "$const" : ObjectId("627f7821c5e9de1b328ea918")
                                        }
                                    ]
                                },
                                {
                                    "$eq" : [
                                        "$deletedAt",
                                        {
                                            "$const" : null
                                        }
                                    ]
                                },
                                {
                                    "$eq" : [
                                        "$date",
                                        {
                                            "$const" : ISODate("2022-05-30T00:00:00.000+0000")
                                        }
                                    ]
                                }
                            ]
                        }
                    },
                    {
                        "date" : {
                            "$_internalExprEq" : ISODate("2022-05-30T00:00:00.000+0000")
                        }
                    },
                    {
                        "deletedAt" : {
                            "$_internalExprEq" : null
                        }
                    },
                    {
                        "groupId" : {
                            "$_internalExprEq" : ObjectId("627f7821c5e9de1b328ea918")
                        }
                    }
                ]
            },
            "nReturned" : 1.0,
            "executionTimeMillisEstimate" : 3.0,
            "works" : 28853.0,
            "advanced" : 1.0,
            "needTime" : 28851.0,
            "needYield" : 0.0,
            "saveState" : 28.0,
            "restoreState" : 28.0,
            "isEOF" : 1.0,
            "direction" : "forward",
            "docsExamined" : 28851.0
        }
    },
    "command" : {
        "aggregate" : "LESSONS",
        "pipeline" : [
            {
                "$match" : {
                    "$expr" : {
                        "$and" : [
                            {
                                "$eq" : [
                                    "$groupId",
                                    ObjectId("627f7821c5e9de1b328ea918")
                                ]
                            },
                            {
                                "$eq" : [
                                    "$deletedAt",
                                    null
                                ]
                            },
                            {
                                "$eq" : [
                                    "$date",
                                    ISODate("2022-05-30T00:00:00.000+0000")
                                ]
                            }
                        ]
                    }
                }
            }
        ],
        "cursor" : {

        },
        "$db" : "TEST_DB"
    },
    "serverInfo" : {
        "host" : "nitro",
        "port" : 2717.0,
        "version" : "5.0.8",
        "gitVersion" : "c87e1c23421bf79614baf500fda6622bd90f674e"
    },
    "serverParameters" : {
        "internalQueryFacetBufferSizeBytes" : 104857600.0,
        "internalQueryFacetMaxOutputDocSizeBytes" : 104857600.0,
        "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600.0,
        "internalDocumentSourceGroupMaxMemoryBytes" : 104857600.0,
        "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600.0,
        "internalQueryProhibitBlockingMergeOnMongoS" : 0.0,
        "internalQueryMaxAddToSetBytes" : 104857600.0,
        "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600.0
    },
    "ok" : 1.0,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1652524646, 1),
        "signature" : {
            "hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1652524646, 1)
}


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Mongoimport error: server returned error on SASL authentication step: BSON field 'saslContinue.mechanism' is an unknown field and tags: ['database-tools']

[User]
Text:I am trying to import some json files to mongodb collections using following command on a server other than mongodb server:It is throwing following error:How ever If I hit the same command on mongodb server itself then it works. I have checked all credentials & other details. All details are correct.updateif I run the command using -vvvv parameter (for verbose output) then I got this:
Code:mongoimport --host IP --port 27017 -u myuser -p mypass --authenticationDatabase admin --db dbname --collection collection --drop --type json --file absolutejsonfilepath --jsonArray
server returned error on SASL authentication step: BSON field 'saslContinue.mechanism' is an unknown field.
2022-05-12T12:29:22.261+0000    checking options
2022-05-12T12:29:22.262+0000            dumping with object check disabled
2022-05-12T12:29:22.262+0000    will listen for SIGTERM, SIGINT, and SIGKILL
2022-05-12T12:29:22.283+0000    got error from options parsing: error connecting to db server: server returned error on SASL authentication step: BSON field 'saslContinue.mechanism' is an unknown field.
2022-05-12T12:29:22.283+0000    Failed: error connecting to db server: server returned error on SASL authentication step: BSON field 'saslContinue.mechanism' is an unknown field.

[Assistant]
Text:What is your mongodb version and mongoimport version?
It could be due to version related issues
Try to use latest mongoimport from mongodb tools
Code:

[User]
Text:Mongodb version: 5.0.8
Mongoimport version: 100.5.2I have latest version of Mongoimport 100.5.2 on my machine.
Code:

[Assistant]
Text:Can you connect to your db from the host where you are trying to run mongoimport?
Are you using any special characters in your password?
If yes try to escape it
or
Does it work with lower version of mongoimport?
Code:

[User]
Text:Can you connect to your db from the host where you are trying to run mongoimport? YES
Are you using any special characters in your password? NO
Does it work with lower version of mongoimport?
I AM NOT ABLE TO DOWNGRADE TO VERSION 100.5.1. THE INSTALLATION STEPS MENTIONED IN THE BELOW LINK GIVES ERROR:
ERROR: dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Code:

[User]
Text:It worked with the mongoimport version 100.5.1. For downgrade issue I removed all db tools from the host using command:After that I force install mongodb tools on the host:After that I tried mongoimport command on the host then it worked.
Code:sudo apt-get --purge remove mongodb-org
sudo apt purge mongodb*
sudo dpkg -i --force-all mongodb-database-tools-ubuntu2004-x86_64-100.5.1.deb


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: How to lookup and filter by date? and tags: ['aggregation', 'queries', 'node-js']

[User]
Text:The code below returns me all data that are present in CollectionA but NOT in CollectionB . (Using the mainID as reference).But now I need to add another filter . I also need to get data where the field createdAt is greater than X days.
in other words: Records that have more than X days of life.Tried using $gte and $lte inside $match but didn’t work.Here is my database:To clarify. I need to return data if:Also tried this, but didnt work. It returns data only when the first condition is meet.Any helpp?
Code:mainID  return this.aggregate([
    {
      $lookup: {
        from: 'CollectionB',
        localField: 'mainId',
        foreignField: 'mainId',
        as: 'validd',
      },
    },
    {
      $match: {
        'validd.mainId': { $exists: false },
      },
    },
  ]);
createdAt$gte$lte$match{
  $match: {
    'validd.createdAt': { $gte: moment.utc().add(-90, "days") },
  },
},
{
  _id: 227dd33c5c51c79f63743da3
  mainId: 5c306562-9c87-48dd-93ca-4a118be50490
  createdAt: 2022-05-07T02:28:12.537+00:00
},
{
  _id: f3ddd33c5c51c79f63743da3
  mainId: 5c306562-9c87-48dd-93ca-4a118be50490
  createdAt: 2022-05-10T02:28:12.537+00:00
},
{
  _id: 227dd33c5c51c79f63743da3
  mainId: 5c306562-9c87-48dd-93ca-4a118be50490
  createdAt: 2022-01-01T02:28:12.537+00:00
}
createdAtreturn this.aggregate([
    {
      $lookup: {
        from: 'CollectionB',
        localField: 'mainId',
        foreignField: 'mainId',
        as: 'validd',
      },
    },
    {
      $match: {
        $expr: {
          $or: [
            {
              $eq: ["$validd", []]
            },
            {
              $and: [
                {
                  $lt: [ "validd.createdAt", moment.utc().add(-interval, "days").format('YYYY-MM-DD') ]
                },
                {
                  $ne: ["validd", null]
                }
              ]
            }
          ]
        }
      },
    },
  ]);

[Assistant]
Text:At first sight I see a few things.You correctly use the dollar sign in$eq: ["$validd", []]but you completely forgo it in the other part of the query.Even with dollar sign, the following will not do what you want.$ne: ["validd", null]I think it should be:because an empty array will match the expression “$ne : [ “$validd” , null ]”, see:This being said about $ne:[validd,null], I do not think you really need it since you are querying validd.createdAt. That last part of the query should only be true if validd contains at least one element with the field createdAt matching your $lt.I do not see anything in your query that requires $expr, so I would try to simplify and do:Untested:
Code:$eq: ["$validd", []]$ne: ["validd", null]$ne : [ "$validd" : [] ]
mongosh> c.find()
{ _id: 0, validd: [] }
{ _id: 1, validd: [ 10 ] }

// the empty array is also matched
mongosh> c.aggregate( { "$match" : { "$expr" : { "$ne" : [ "$validd" , null ] }}})
{ _id: 0, validd: [] }
{ _id: 1, validd: [ 10 ] }

// but with you only get the non-empty array, which is inline with your intent
mongosh> c.aggregate( { "$match" : { "$expr" : { "$ne" : [ "$validd" , [] ] }}})
{ _id: 1, validd: [ 10 ] }
match_stage = { "$match" : {
  "$or" :
  [
    { "validd" : [] } ,
    {
      "validd.createdAt" : { "$lt" : moment.utc().add(-interval, "days").format('YYYY-MM-DD')  }
    }
  ]
} }

[User]
Text:Hi, Steeve. Thank you very much for your explanation.I tried your snippet, and I still get the same results.Collection A:Collection B:Then I run the search using an interval of 5 days
We can see that:But the output of the query only returns me the first condition. Only First because it doesn’t exists on CollectionB. But I also need to return those that exists but have a createdAt longer than my interval.
Code:name: "first"
mainId: "03d36f3e-535e-4074-aefa-f4b09fa5eba2"
createdAt: 2022-05-10T11:38:08.284+00:00

name: "second"
mainId: "13d36f3e-535e-4074-aefa-f4b09fa5eba2"
createdAt: 2022-05-10T11:38:13.284+00:00

name: "third"
mainId: "23d36f3e-535e-4074-aefa-f4b09fa5eba2"
createdAt: 2022-05-10T11:38:46.284+00:00
name: second
mainId: "13d36f3e-535e-4074-aefa-f4b09fa5eba2"
createdAt: 2022-05-10T11:38:13.284+00:00

name: third
mainId: "23d36f3e-535e-4074-aefa-f4b09fa5eba2"
createdAt: 2022-05-04T11:38:13.284+00:00
firstthirdCreatedAt = 2022-05-04secondFirstcreatedAtinterval

[User]
Text:Sorry for adding another reply (can’t edit previous one).Our solution worked with an extra.Not sure WHY momentJS wasn’t able to do the job.
I had to add a new Date:
Code:momentJSnew Date$match: {
  $or: [
    { "eligible": [] },
    { "eligible.createdAt": { $lt: new Date(interval) } }
  ]
}


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: Mongo 5+ on Apple M1 laptops via Docker and tags: ['containers', 'installation']

[User]
Text:I just upgraded my development machine to an M1 MacBook Pro, and immediately discovered that my mongodb docker containers weren’t working. I noticed that there isn’t yet a build of mongodb for the M1, and I found the relevant ticket for the upgrade which doesn’t look like it’ll be addressed any time soon (backlogged P3). This would be fine if mongodb ran under Rosetta, but with v5 an AVX requirement was introduced. Rosetta does not simulate AVX.I find myself stuck. Do I have to roll back to mongo4? Is there any way to get an M1 build prioritised?
Code:

[Assistant]
Text:@Michael_Townsend - Let’s continue the discussion from https://jira.mongodb.org/browse/SERVER-42427 here.In your environment, which binaries are being run within the Docker container? Is Docker compiled natively for M1 or is it itself running under Rosetta 2? I suspect that what may be happening is that Docker is trying to run the x86_64 linux MongoDB 5.0 binaries inside the Docker container, and those do indeed require AVX, which Rosetta 2 doesn’t emulate.But I’m puzzled why Docker running on an M1 would be emulating an x86 environment. Shouldn’t it be running an ARM linux, and then using the ARM linux MongoDB in that, etc.?
Code:

[Assistant]
Text:Hello @Michael_Townsend!Would you by chance be interested in trying to run MongoDB 5.0 Natively on your M1 Mac?I am sorry for any frustration this has all caused you, but I may have an acceptable work around for the time being.The way to install 5.0 Natively without Docker on an M1 Mac:To check and verify that it is running:If you have you have any issues with this work around, please let me know. As I would be happy to help you situate this. However, if you must have MongoDB 5.0 within a Docker container for work requirements that’s is completely understandable. I am merely giving you another option to use MongoDB 5.0 on your M1 Mac while compatibility developments using Docker are sorted out.I would also love to hear about your applications stack and use case(s) for MongoDB on your M1 Mac like my colleague Andrew would.Anything else that we can help you with in the meantime, please let us know.Regards,Garrett
Code:

[Assistant]
Text:Hi GarrettI would be happy to try out MongoDB 5.0 natively (without Docker) on my Apple M1 Max running the latest macOS Monterey.When you say “natively” due you mean true native or still via Rosetta 2 ?regards
Matt
Code:

[Assistant]
Text:Welcome to the MongoDB Community Forums @Matthew_Donaldson!At the moment, running natively on Apple M1 is referring to using Rosetta 2 without Docker (for example, installing the macOS x86_64 binaries via brew). The macOS packages are working fine for me on M1 with Rosetta 2 installed.This discussion was originally about someone trying to run the Linux x86_64 binaries in Docker on M1, which will be problematic because of the requirement for AVX support in MongoDB 5.0 packages for Linux x86_64. The solution for the original question would be to either install MongoDB 4.4 packages on Linux (since those are not optimised for AVX) or to build MongoDB 5.x from source with an older x86_64 CPU architecture target.Building the MongoDB server with ARM64/aarch64 support for MacOS  (SERVER-50115) is currently blocked pending resolution of a SpiderMonkey JavaScript engine upgrade (SERVER-42427).Regards,
Stennie
Code:brewx86_64


[System] You are an assistant of MongoDB Community who is responsible for answering the questions related with MongoDB.

[System] The following conversation has title: MongoDB Docker container not starting properly and tags: ['replication', 'containers', 'storage']

[User]
Text:Hi All,
Sorry if this is the wrong category.I have inherited the infrastructure from someone who is long gone from my company. I am not aware of the configuration/steps used to bringup the MongoDB containers.Basically, we are running MongoDB (v3.6.18) as three containers and are configured as a replicaset. When the docker stack is deployed, two of the containers are up and running, but the third one is taking a long time to come up. The two DBs that are up, are about 1GB each. The third DB is about 637GB. Since the third DB is large, it tries for about 3.5hrs and exits and tries to be recreated. This goes on a loop.The logs from the other two DBs which are up show that they try to reach the other DBs and fails. Similar logs from both DB01 and DB02.For DB03, the following are the initial set of logsWhen I logged into either the DB01 or DB02 containers and check the status, it shows the followingThe docker compose file isand there is a rs.initiate file which I am sure is used for configuring the replicaset, but not sure how.Please let me know how I can recover the MongoDB stack so that the containers are all up and running properly.
Let me know if you need any more information.
Code:**docker logs <DB01_container_ID>**
<snip>
2022-04-28T00:55:23.546+0000 I CONTROL  [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Could not find host matching read preference { mode: "primary" } for set graylog
2022-04-28T00:59:58.027+0000 I NETWORK  [LogicalSessionCacheRefresh] Starting new replica set monitor for graylog/mongodb_db01:27017,mongodb_db02:27018,mongodb_db03:27019
2022-04-28T01:00:03.030+0000 W NETWORK  [LogicalSessionCacheRefresh] Failed to connect to 10.0.11.2:27017 after 5000ms milliseconds, giving up.
2022-04-28T01:00:03.033+0000 W NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 10.0.11.5:27018 after 5000ms milliseconds, giving up.
2022-04-28T01:00:08.033+0000 W NETWORK  [LogicalSessionCacheRefresh] Failed to connect to 10.0.11.8:27019 after 5000ms milliseconds, giving up.
2022-04-28T01:00:08.033+0000 W NETWORK  [LogicalSessionCacheRefresh] Unable to reach primary for set graylog
2022-04-28T01:00:08.033+0000 I NETWORK  [LogicalSessionCacheRefresh] Cannot reach any nodes for set graylog. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2022-04-28T01:00:13.539+0000 W NETWORK  [LogicalSessionCacheRefresh] Failed to connect to 10.0.11.8:27019 after 5000ms milliseconds, giving up.
2022-04-28T01:00:18.545+0000 W NETWORK  [LogicalSessionCacheRefresh] Failed to connect to 10.0.11.2:27017 after 5000ms milliseconds, giving up.
2022-04-28T01:00:23.551+0000 W NETWORK  [LogicalSessionCacheRefresh] Failed to connect to 10.0.11.5:27018 after 5000ms milliseconds, giving up.
2022-04-28T01:00:23.551+0000 W NETWORK  [LogicalSessionCacheRefresh] Unable to reach primary for set graylog
2022-04-28T01:00:23.551+0000 I NETWORK  [LogicalSessionCacheRefresh] Cannot reach any nodes for set graylog. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.
2022-04-28T01:00:23.551+0000 I CONTROL  [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Could not find host matching read preference { mode: "primary" } for set graylog
<snip>
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27019 dbpath=/data/db 64-bit host=01d5b6a43712
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] db version v3.6.18
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] git version: 2005f25eed7ed88fa698d9b800fe536bb0410ba4
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] modules: none
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] build environment:
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten]     distarch: x86_64
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2022-04-28T00:08:14.735+0000 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIpAll: true, port: 27019, ssl: { CAFile: "/etc/certs/ca.pem", PEMKeyFile: "/etc/certs/certandkey.pem", allowConnectionsWithoutCertificates: true, allowInvalidHostnames: true, mode: "preferSSL" } }, replication: { oplogSizeMB: 400, replSetName: "graylog" } }
2022-04-28T00:08:14.737+0000 W -        [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2022-04-28T00:08:14.741+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2022-04-28T00:08:14.743+0000 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
2022-04-28T00:08:14.743+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=63873M,cache_overflow=(file_max=0M),session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),compatibility=(release="3.0",require_max="3.0"),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
[root@dcvsl126 sjillalla]# docker exec -it 24664c0d5a58 mongo
MongoDB shell version v3.6.18
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("964473e9-b60b-46b9-b1d1-de99829f62a4") }
MongoDB server version: 3.6.18
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2022-04-27T20:09:57.767+0000 I CONTROL  [initandlisten]
2022-04-27T20:09:57.767+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-04-27T20:09:57.767+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2022-04-27T20:09:57.767+0000 I CONTROL  [initandlisten]
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten]
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] ** WARNING: You are running on a NUMA machine.
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] **          We suggest launching mongod like this to avoid performance problems:
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] **              numactl --interleave=all mongod [other options]
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten]
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten]
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2022-04-27T20:09:57.769+0000 I CONTROL  [initandlisten]
graylog:OTHER> rs.status()
{
        "state" : 10,
        "stateStr" : "REMOVED",
        "uptime" : 16290,
        "optime" : {
                "ts" : Timestamp(1650247277, 4),
                "t" : NumberLong(111)
        },
        "optimeDate" : ISODate("2022-04-18T02:01:17Z"),
        "lastHeartbeatMessage" : "",
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "infoMessage" : "",
        "ok" : 0,
        "errmsg" : "Our replica set config is invalid or we are not a member of it",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig",
        "operationTime" : Timestamp(1650247277, 4),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1650247277, 4),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
graylog:OTHER> exit
version: '3'
services:
  db01:
    image: docker-prod.tools.royalsunalliance.ca/mongo:3.6.18
    volumes:
    - /docker/services/mongodb/db01:/data/db
    - /docker/services/mongodb/db01-dump:/data/db/dump
    - /docker/services/mongodb/db01-config/mongod.conf:/etc/mongod.conf
    - /docker/services/elasticsearch-prod/certs/db01:/etc/certs
    ports:
    - "27017:27017"
    command: ["mongod", "--sslAllowConnectionsWithoutCertificates", "--sslMode", "preferSSL", "--sslPEMKeyFile", "/etc/certs/certandkey.pem", "--sslCAFile", "/etc/certs/ca.pem", "--config", "/etc/mongod.conf", "--sslAllowInvalidHostnames"]

  db02:
    image: docker-prod.tools.royalsunalliance.ca/mongo:3.6.18
    volumes:
    - /docker/services/mongodb/db02:/data/db
    - /docker/services/mongodb/db02-dump:/data/db/dump
    - /docker/services/mongodb/db02-config/mongod.conf:/etc/mongod.conf
    - /docker/services/elasticsearch-prod/certs/db02:/etc/certs
    ports:
    - "27018:27018"
    command: ["mongod", "--port", "27018", "--sslAllowConnectionsWithoutCertificates", "--sslMode", "preferSSL", "--sslPEMKeyFile", "/etc/certs/certandkey.pem", "--sslCAFile", "/etc/certs/ca.pem", "--config", "/etc/mongod.conf", "--sslAllowInvalidHostnames"]
    #command: ["mongod", "--config", "/etc/mongod.conf"]

  db03:
    image: docker-prod.tools.royalsunalliance.ca/mongo:3.6.18
    volumes:
    - /docker/services/mongodb/db03:/data/db
    - /docker/services/mongodb/db03-dump:/data/db/dump
    - /docker/services/mongodb/db03-config/mongod.conf:/etc/mongod.conf
    - /docker/services/elasticsearch-prod/certs/db03:/etc/certs
    ports:
    - "27019:27019"
    command: ["mongod", "--port", "27019", "--sslAllowConnectionsWithoutCertificates", "--sslMode", "preferSSL", "--sslPEMKeyFile", "/etc/certs/certandkey.pem", "--sslCAFile", "/etc/certs/ca.pem", "--config", "/etc/mongod.conf", "--sslAllowInvalidHostnames"]
rs.initiate({
  "_id": "graylog",
  "version": 1,
  "members" : [
   {"_id": 1, "host": "mongodb_db01:27017"},
   {"_id": 2, "host": "mongodb_db02:27018"},
   {"_id": 3, "host": "mongodb_db03:27019"}
  ]
 })

[Assistant]
Text:Hi @Sarojini_Jillalla
Welcome to the community forum!!Could you help with a few information in regard to the concern.Was this setup in working condition before the issue started to arrive? If yes, could you please help in understanding the changes done in between due to which the issue was seen.Could you share the contents of the mongod.conf file  for the replica sets?Was there any change done to the docker compose file when in working condition to now, if yes, could you please share the original docker file.Lastly would suggest to scratch if the database does not contain important data o if the backup for the deployment  is already available.Please let me know the following details to assist you better.Thanks
Aasawari
Code:mongod.conf

[User]
Text:Hi @Aasawari,Please find the answers below.for all the dbs.
3. There is no change to the docker-compose.yml file.Since the DB03 contains huge data (about 640GB), I did not want to tinker with it.Please let me know if you need any more information.Thanks,
Sarojini Jillalla
Code:replication:
   oplogSizeMB: 400
   replSetName: graylog