[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 include another key in array object into array aggregate and tags: ['aggregation']

[User]
Text:I get difficult to add key into the aggregate from array objecthere is the data which I want to aggregateand the result I wantin my case here I am using Go, but I need the vanilla Mongo Query for this so I can translate into Go query pipeline
Code:  // COMPANY
   {
     "name": "Note Inc",
     "shortName": "The Note",
     "members": [
        {
          "_id": ObjectId("5efdac62cd210f3b658d98f6"),
          "status": false,
          "role": "chairman",
        },
        {
          "_id": ObjectId("5effac62dd210f9b658d98f1"),
          "status": true,
          "role": "admin",
        }
     ]
   }
   // COMPANY after aggregate the Person
   {
     "name": "Note Inc",
     "shortName": "The Note",
     "members": [
        {
          "_id": ObjectId("5efdac62cd210f3b658d98f6"),
          "name": "Shinta",
          "gender": "female"
          "status": false,
          "role": "chairman",
        },
        {
          "_id": ObjectId("5effac62dd210f9b658d98f1"),
          "name": "John",
          "gender": "male"
          "status": true,
          "role": "admin",
        }
     ]
   }

[Assistant]
Text:Ok, in the output you want to have 2 additional fields: ‘name’ and ‘gender’.
Where are you going to take that data from? Is it from sibling collection? Provide a document example, if the answer is ‘yes’.
Code:

[User]
Text:yess that output was from the Person ID , so the members._id is trigger to Person ID  ( reference )
here is the document Person
Code:{
    "_id": ObjectId("5effac62dd210f9b658d98f1"),
    "email": "test@admin.com",
    "phoneNumber": "",
    "firstName": "John",
    "gender": "male"
}

[Assistant]
Text:This aggregation should give you what you need:Tested on this dataset:
Code:db.members.aggregate([
  {
    $addFields: {
      ids: {
        $map: {
          input: '$members',
          in: '$$this._id',
        }
      }
    }
  },
  {
    // $lookup will be executed once per member document
    $lookup: {
      from: 'persons',
      localField: 'ids',
      foreignField: '_id',
      as: 'persons',
    }
  },
  // cleanup
  {
    $unset: ['ids'],
  },
  {
    $addFields: {
      mixed: {
        // order of array here matters for $group stage
        $concatArrays: ['$members', '$persons']
      }
    }
  },
  {
    $unwind: '$mixed',
  },
  {
    $group: {
      _id: {
        _id: '$mixed._id',
        rootId: '$_id',
        name: '$name',
        shortName: '$shortName',
      },
      // down below:
      // - use $first to read the prop from 'person' document
      // - use $last to read the prop from 'members' document
      name: {
        $last: '$mixed.firstName',
      },
      gender: {
        $last: '$mixed.gender',
      },
      status: {
        $first: '$mixed.status',
      },
      role: {
        $first: '$mixed.role',
      }
    }
  },
  {
    $group: {
      _id: '$_id.rootId',
      name: {
        $first: '$_id.name',
      },
      shortName: {
        $first: '$_id.shortName',
      },
      members: {
        $push: {
          _id: '$_id._id',
          name: '$name',
          gender: '$gender',
          status: '$status',
          role: '$role',
        }
      }
    }
  },
  // cleanup
  {
    $unset: '_id',
  }
]);
db.members.insertMany([
  {
    "name": "Note Inc A",
    "shortName": "The Note A",
    "members": [
      {
        "_id": ObjectId("5effac62dd210f9b658d98f1"),
        "status": false,
        "role": "chairman",
      },
      {
        "_id": ObjectId("5efdac62cd210f3b658d98f6"),
        "status": true,
        "role": "admin",
      }
    ]
  },
  {
    "name": "Note Inc B",
    "shortName": "The Note B",
    "members": [
      {
        "_id": ObjectId("5efdac62cd210f3b65f68d98"),
        "status": false,
        "role": "hr",
      },
    ]
  }
]);

db.persons.insertMany([
  {
    "_id": ObjectId("5effac62dd210f9b658d98f1"),
    "firstName": "John",
    "gender": "male",
  },
  {
    "_id": ObjectId("5efdac62cd210f3b658d98f6"),
    "firstName": "Shinta",
    "gender": "female",
  },
  {
    "_id": ObjectId("5efdac62cd210f3b65f68d98"),
    "firstName": "Sara",
    "gender": "female",
  },
]);

[User]
Text:I updated the document
Code:

[User]
Text:is this is can’t be done by $project ?
Code:

[Assistant]
Text:Simply using $project is not enough.Here is the minimal aggregation, that you can have to get what you need:But this one will probably be less performant, as it will make $lookup per each item in ‘members’ array.
Code:db.members.aggregate([
  {
    $unwind: {
      path: '$members',
      preserveNullAndEmptyArrays: true,
    }
  },
  {
    $lookup: {
      from: 'persons',
      localField: 'members._id',
      foreignField: '_id',
      as: 'person',
    }
  },
  {
    $unwind: '$person',
  },
  {
    $addFields: {
      'members.name': '$person.firstName',
      'members.gender': '$person.gender',
    },
  },
  {
    $group: {
      _id: '$_id',
      name: {
        $first: '$name',
      },
      shortName: {
        $first: '$name',
      },
      members: {
        $push: '$members',
      }
    }
  }
]);

[Assistant]
Text:@slava, @Virtual_Database, I tried this aggregation. Let me know how it works for you.
Code:db.company.aggregate([
  {
      $lookup: {
          from: "person",
          localField: "members._id",
          foreignField: "_id",
          as: "company_persons"
      }
  },
  {
      $addFields: {
          company_persons: 0,
          members: {
              $map: {
                  input: "$members", as: "mem",
                  in: {
                      $let: {
                           vars: {
                               varin: {
                                   $arrayElemAt: [ { $filter: {
                                                        input: "$company_persons", as: "per",
                                                        cond: { $eq: [ "$$per._id", "$$mem._id" ] }
                                    } }, 0 ]
                                }
                           },
                           in: {
                               $mergeObjects: [ "$$mem", {  firstName: "$$varin.firstName", gender: "$$varin.gender" } ]
                           }
                      }
                  }
              }
          }
      }
  }
]).pretty()

[User]
Text:the latest minimal query was work well, what different ? will the latest minimal query gonna has affect ??
Code:

[User]
Text:the previous query u show was , I got : (AtlasError) _id is not allowed in this atlas tier
Code:(AtlasError) _id is not allowed in this atlas tier

[User]
Text:this one is worked also, btw, can I just filter one person id ?like say I want just filter person where ID = ObjectId(“PERSON_ID”) so I dont want to show all members on the list
Code:db.company.aggregate([
  {
      $lookup: {
          from: "person",
          localField: "members._id",
          foreignField: "_id",
          as: "company_persons"
      }
  },
  {
      $addFields: {
          company_persons: 0,
          members: {
              $map: {
                  input: "$members", as: "mem",
                  in: {
                      $let: {
                           vars: {
                               varin: {
                                   $arrayElemAt: [ { $filter: {
                                                        input: "$company_persons", as: "per",
                                                        cond: { $eq: [ "$per._id", "$mem._id" ] }
                                    } }, 0 ]
                                }
                           },
                           in: {
                               $mergeObjects: [ "$mem", {  firstName: "$varin.firstName", gender: "$varin.gender" } ]
                           }
                      }
                  }
              }
          }
      }
  }
]).pretty()

[Assistant]
Text:can I just filter one person id ?Yes, you can (with the same aggregation and some changes):
Code:var INPUT_PERSON = ObjectId("5efdac62cd210f3b658d98f6") // or ObjectId("5effac62dd210f9b658d98f1")

db.company.aggregate( [
  {
      $lookup: {
          from: "person",
          localField: "members._id",
          foreignField: "_id",
          as: "company_persons"
      }
  },
  {
      $addFields: {
          members: {
              $reduce: {
                  input: "$members", initialValue: [ ],
                  in: {
                      $let: {
                           vars: {
                               match: {
                                   $arrayElemAt: [ { $filter: {
                                                        input: "$company_persons", as: "per",
                                                        cond: { $eq: [ "$$per._id", "$$this._id" ] }
                                    } }, 0 ]
                                }
                           },
                           in: {
                               $cond: [ { $eq: [ INPUT_PERSON, "$$this._id" ] },
                                        [ { $mergeObjects: [ "$$this", { firstName: "$$match.firstName", gender: "$$match.gender" } ] } ],
                                        "$$value"
                               ]
                           }
                      }
                  }
              }
          }
      }
  },
  {
      $project: { company_persons: 0 }
  }
] ).pretty()


[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 on dateTime and tags: []

[User]
Text:Hi team,can anyone help me to find a best solution for the following performance related problem.?i have collection with fields
_id
sensorId
dateTime(including timestamp)
value1
value2this collection has millions of data and also its increasing day by day. The query execution time is increasing day by day. Most of the time(more that 90%) i called this collection with sensorId and dateTime(with $gt or $lt or both). So, i think it should be better to create a compound index with sensorId and dateTime. Really its gives good result. but, the index size is increased dramatically. So, is it a good method? can anyone make good suggestion on this?
Code:

[Assistant]
Text:Hi Renjith,Welcome to the community. As I understand it, you’re facing query performance issues, tried to solve it using a compound index, but you are currently concerned by the index sizes. Is this accurate?It would help if you can provide more context:In general, having indexes to back your queries is a recommended practice in all databases, not just MongoDB. See Create Indexes to Support Your Queries for some examples.Best regards
Kevin
Code:db.collection.explain('executionStats').find(...)

[User]
Text:Thank Kevin for your reply.Yes, you are right. My problem was that.This is our test database result. same as original. Right now we have 360K documents in our data. Using this index i can fetch data correctly with good performance. but issue will be in future within months. 100X more data are coming. So, the index size will increase dramatically.
Screen Shot 2020-06-28 at 11.39.17 AM1663×765 78.8 KBAll my documents are in same structure. Sample one
{
“_id”:{"$oid":“5e813859eea99fb8a1cdcd1a”},
“sensorId”:“e00fce685dc2dae3d35b2054”,
“dateTime”:{"$date":“2020-03-30T00:07:53.000Z”},
“pH”:3.928,
“temperature”:14.9,
“capTemperature”:15,
“sg”:1.195
}Please take a look on the image. As i said my concern is increase of index size. Now our DB has less data but by next it will increase up to 100X and will increase dramatically after that. As per my calculation, if i use this compound index, index size of the particular compound index will be 20% of the actual data.Version 4.0.19, M20 ClusterThanks in advance
Code:

[User]
Text:Output of question number 3Screen Shot 2020-06-28 at 11.41.08 AM1025×345 39.5 KB
Code:

[Assistant]
Text:Hi Renjith,The index you have is optimal for your query, so if your collection gets much larger, the query still should return relatively quick (with caveat of data size vs. hardware capabilities, of course).I see you have two indexes: the mandatory _id index and the compound index. The compound index itself is 3MB, which is a bit less than 10% of the collection size.As with most things in computing, it is a balancing act. If you feel that returning queries quickly in a large collection is worth the space tradeoff, then I encourage you to keep the compound index. Case in point, your first screenshot shows that in a collection of 268k documents, the query returned in just 5ms. For most use cases, this space vs. speed tradeoff is worth it.However, if you feel that you can tolerate much slower query response time and if space is much more important to you, then dropping the index is an option.Another option is to create an index only on sensorId, so your query won’t be totally unindexed.All three options have advantages & disadvantages, so it’s up to you to choose which option is “best” for your use case.Having said that, be aware that as your collection gets larger, scanning unnecessary documents to return a query will have other side effect as well, such as pushing your working set off memory and could possibly make everything slower (since irrelevant documents are constantly being paged into memory to be examined, only to be thrown out again).Best regards,
Kevin
Code:_idsensorId

[User]
Text:Great. Thanks for the reply Kevin
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: Foreign key constraint and tags: ['data-modeling']

[User]
Text:Is there any way restrict document delete in mongodb if it is being as refrence to any other docuement(s) (something like foreign key constraint in mysql) or is there any other work around for this functionality.
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: How to perform multiple group bys in a aggregation query and tags: ['aggregation']

[User]
Text:Hope you can help I am new to aggregation queries.I have a nested data structure that I want to group to produce statistical output. I have a set of orders where the order is for a country, product and product code. An order looks like:I have an aggregation query that groups by country, product and code.This produces output such as:I want to group this data by country and then product and then code so, for example, Japan would have a list of products i.e. Product 4, Product 2 inside each there would be a list of codes so “Product 4”: [“Code 8”,“Code 5”,“Code 3”,“Code 6”,“Code 2”] etc. Since an order can be made for a Product with a particular code more than once for a country I need I think a map of codes and the counts for each code.
Code:db.orders.findOne();
{
    "_id" : ObjectId("5efc6db38cb109193e41c4d3"),
    "createdDate" : ISODate("2020-06-25T02:06:25.428Z"),
    "data" : {
        "nested" : {
            "country" : "France"
        },
        "product" : "Product 4",
        "latest" : {
            "sub" : {
                "code" : "Code 3"
            }
        }
    }
}
db.getCollection('orders').aggregate([
{
    $unwind :{
        path: "$data.nested.country"
    }
},
{
   $group: {
       _id: { country: "$data.nested.country", product: "$data.product", code: "$data.latest.sub.code" }
   }
}
])
{ "_id" : { "country" : "Slovenia", "product" : "Product 3", "code" : "Code 7" } }
{ "_id" : { "country" : "Japan", "product" : "Product 1", "code" : "Code 9" } }
{ "_id" : { "country" : "Japan", "product" : "Product 4", "code" : "Code 4" } }
{ "_id" : { "country" : "China", "product" : "Product 1", "code" : "Code 1" } }
{ "_id" : { "country" : "France", "product" : "Product 3", "code" : "Code 4" } }
{ "_id" : { "country" : "Japan", "product" : "Product 4", "code" : "Code 8" } }
{ "_id" : { "country" : "Japan", "product" : "Product 4", "code" : "Code 5" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 4", "code" : "Code 4" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 4", "code" : "Code 7" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 4", "code" : "Code 2" } }
{ "_id" : { "country" : "China", "product" : "Product 4", "code" : "Code 8" } }
{ "_id" : { "country" : "France", "product" : "Product 4", "code" : "Code 4" } }
{ "_id" : { "country" : "Japan", "product" : "Product 2", "code" : "Code 3" } }
{ "_id" : { "country" : "Japan", "product" : "Product 2", "code" : "Code 6" } }
{ "_id" : { "country" : "Japan", "product" : "Product 2", "code" : "Code 3" } }
{ "_id" : { "country" : "Slovenia", "product" : "Product 2", "code" : "Code 9" } }
{ "_id" : { "country" : "China", "product" : "Product 2", "code" : "Code 6" } }
{ "_id" : { "country": "Japan", products: [{"product":"Product 2","codes":[{"code":"Code 3","count":2},{"code":"Code 6","count":1]}]

[Assistant]
Text:Hello, @Sean_Barry! Welcome to the community!You can achieve what you want with two sequential $group stagesSample dataset:Aggregation output on sample dataset:
Code:db.orders.aggregate([
  {
    $group: {
      _id: {
        country: '$data.nested.country',
        product: '$data.product',
      },
      productCodes: {
        $push: '$data.latest.sub.code',
      },
      uniqueCodes: {
        $addToSet: '$data.latest.sub.code',
      }
    }
  },
  {
    $group: {
      _id: '$_id.country',
      country: {
        $first: '$_id.country',
      },
      products: {
        $push: {
          product: '$_id.product',
          codes: {
            $map: {
              // run $filter+$size operations per each code
              input: '$uniqueCodes',
              as: 'code',
              in: {
                code: '$$code',
                count: {
                  $size: {
                    $filter: {
                      // collect same codes into one array
                      // to be able to count them per product
                      input: '$productCodes',
                      cond: {
                        $eq: ['$$code', '$$this'],
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
  // cleanup
  {
    $unset: ['_id'],
  }
]);
db.orders.insertMany([
  {
    '_id' : 1,
    'data' : {
      'nested' : {
        'country' : 'France'
      },
      'product' : 'Product 1',
      'latest' : {
        'sub' : {
          'code' : 'Code A'
        }
      }
    }
  },
  {
    '_id' : 2,
    'data' : {
      'nested' : {
        'country' : 'France'
      },
      'product' : 'Product 2',
      'latest' : {
        'sub' : {
          'code' : 'Code B'
        }
      }
    }
  },
  {
    '_id' : 3,
    'data' : {
      'nested' : {
        'country' : 'Canada'
      },
      'product' : 'Product 1',
      'latest' : {
        'sub' : {
          'code' : 'Code B'
        }
      }
    }
  },
  {
    '_id' : 4,
    'data' : {
      'nested' : {
        'country' : 'Ukraine'
      },
      'product' : 'Product 2',
      'latest' : {
        'sub' : {
          'code' : 'Code B'
        }
      }
    }
  },
  {
    '_id' : 5,
    'data' : {
      'nested' : {
        'country' : 'Canada'
      },
      'product' : 'Product 1',
      'latest' : {
        'sub' : {
          'code' : 'Code A'
        }
      }
    }
  },
  {
    '_id' : 6,
    'data' : {
      'nested' : {
        'country' : 'Canada'
      },
      'product' : 'Product 1',
      'latest' : {
        'sub' : {
          'code' : 'Code A'
        }
      }
    }
  }
]);
[
  {
    "country" : "France",
    "products" : [
      {
        "product" : "Product 2",
        "codes" : [
          {
            "code" : "Code B",
            "count" : 1
          }
        ]
      },
      {
        "product" : "Product 1",
        "codes" : [
          {
            "code" : "Code A",
            "count" : 1
          }
        ]
      }
    ]
  },
  {
    "country" : "Canada",
    "products" : [
      {
        "product" : "Product 1",
        "codes" : [
          {
            "code" : "Code B",
            "count" : 1
          },
          {
            "code" : "Code A",
            "count" : 2
          }
        ]
      }
    ]
  },
  {
    "country" : "Ukraine",
    "products" : [
      {
        "product" : "Product 2",
        "codes" : [
          {
            "code" : "Code B",
            "count" : 1
          }
        ]
      }
    ]
  }
]

[User]
Text:Thank you very much. That’s great. Works perfectly. I couldn’t have come up with this solution. There’s definitely a lot going on. I thought I might just need another $group.
Code:db.orders.aggregate([
  {
    $group: {
      _id: {
        country: '$data.nested.country',
        product: '$data.product',
      },
      productCodes: {
        $push: '$data.latest.sub.code',
      },
      uniqueCodes: {
        $addToSet: '$data.latest.sub.code',
      }
    }
  },
  {
    $group: {
      _id: '$_id.country',
      country: {
        $first: '$_id.country',
      },
      products: {
        $push: {
          product: '$_id.product',
          codes: {
            $map: {
              // run $filter+$size operations per each code
              input: '$uniqueCodes',
              as: 'code',
              in: {
                code: '$code',
                count: {
                  $size: {
                    $filter: {
                      // collect same codes into one array
                      // to be able to count them per product
                      input: '$productCodes',
                      cond: {
                        $eq: ['$code', '$this'],
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
  // cleanup
  {
    $unset: ['_id'],
  }
]);


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

[System] The following conversation has title: "Conditional" object update and tags: []

[User]
Text:Hi everyone!Before describing my issue I’d like to give you a bit of context. I am working on a GraphQL server coded in Rust that uses MongoDB Rust crate as a Database driver. One of the main goals of this project is to have a very safe permissions system.My problem is related to write permissions.
Let’s say I have the following object:When a user tries to update this type of object, before the server actually updates it, it needs to make sure that:Now the problem with this is that it requires 2 Database interactions, a find_one for step 1 (to match the object itself) and a find_one_and_update for step 2 (to match its permissions to the user permissions and update if they are correct).
But this means that if user A is trying to update an object, and user B is trying to delete the same object, user B delete request might go off between user A step 1 and step 2 of its update. And that would lead to an “insufficient permissions” error when in reality the object does not exist.So what I wanted to do was generating a guid before making the update, and then adding it to the updated object to check the result and ensure that the update went through (this would remove step 1 from the process and allow use to avoid mutex in our code since it is a big performance decrease). But this requires to be able to make a conditional update based on the field value and from what I’ve read there is no way to do that ($cond is not available as an update operator: https://docs.mongodb.com/manual/reference/operator/update-field/).So my question is would there be a way to have some sort of field-based conditional object update using MongoDB? I also wonder if there would be a way to find a solution to my problem using aggregation pipelines but I haven’t found much success in that either.
Code:{
    "_id" : ObjectId("5ed0d70c009dab1100a6209c"),
    "name" : "toto",
    "permissions" : {
        "read" : [
            "read1"
        ],
        "write" : [
            "write1"
        ],
    },
}
find_onefind_one_and_updateguid$cond

[Assistant]
Text:Hello, @Thomas_Plisson! Welcome to the community!But this means that if user A is trying to update an object, and user B is trying to delete the same object, user B delete request might go off between user A step 1 and step 2 of its update. And that would lead to an “insufficient permissions” error when in reality the object does not exist.Due to the concurrency docs, write operations will be put in a queue and execute one by one on Mongo server. That means, it delete operation was issued after update operation, it will execute after update operation.would there be a way to have some sort of field-based conditional object update using MongoDB?Since Mongo v4.2, update operations can use aggregation pipelines for updates. That means, that you can use $cond inside it:In my example, I used updateOne() method, because it returns convenient for you situation result:This output means that 1 document matched by provided query, but it was not updated - either it already had the required value before update (it was modified long time ago or just now by another user) or because user has not enough permissions (due to the code logic).You can achieve then same with .findOneAndUpdate() method and { returnNewDocumet: true } option by comparing your value with the one returned with method.You can not differentiate those two cases (already same value, not enough permissions) using this approach, especially if there is a high chance that same value can be updated by multiple users.The thing is, that you’re trying to assign too much work on 1 operation, such over-optimizations can lead to code complications. In your case, it would be totally OK to have 2 requests do database:You may end up with only 1 request anyway, if user has not enough permissions or the document already had desired value
Code:db.test1.updateOne({ _id: ObjectId("5ed0d70c009dab1100a6209c")}, [
  {
    $set: {
      name: {
        $cond: {
          if: {
            // check if user has required permission
            $in: [userPerm, '$permissions.write']
          },
          then: 1, // overwrite prop value
          else: '$name', // return origin value
        }
      }
    }
  }
]);
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }


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

[System] The following conversation has title: Mongod is not started and tags: ['containers']

[User]
Text:I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OKplease help:
MOngodb 4.0 version
replica set : this node was primary, not no node of replica set is starting.
Code:

[Assistant]
Text:Hi @Aayushi_Mangal, it’s great to see you around here in the community forums.Interestingly enough I just had my MongoDB Docker container crash and when I restarted MongoDB I had the same message in my log file. Even with the message however the mongod process started and continued to run. Are you seeing other errors in the log files?You can remove the FTDC folder as it just contains diagnostic data that may or may not be useful to you.replica set : this node was primary, not no node of replica set is starting.Are you saying that none of your replica set members are starting? The failure that the PRIMARY node had with FTDC shouldn’t have caused problems with the other nodes. Again, what errors are you seeing in the log files for the nodes that are not starting?
Code:mongod

[User]
Text:W NETWORK [ReplicaSetMonitor-TaskExecutor] Unable to reach primary for set repl01Yes, no primary node found, and other replication member is not restarting because, of priority 0 is set .as a last resort, i have executed mongod with --repair.
Code:

[Assistant]
Text:The default config file included when installing MongoDB via a package manager typically sets  /data/db  as the database path./var/lib/mongodb  is where your mongodb installation files are (I assume).Take a look at your config file and see if you can findThat path needs to point to where your MongoDB server was storing its data files. If that isn’t /data/db , you need to figure out where your mongo files were stored.If you were running MongoDB 3.2+, then try searching your filesystem for  WiredTiger.wt . I’m not sure what to look for if you were running  mmap  as the storage engine.
Code:/data/db/var/lib/mongodbstorage:
    dbPath: <Path>
WiredTiger.wtmmap

[User]
Text:Hello @Alfred_Williams,Thank you for your response.
Yes, using 4.0 version, but could you please explain why to look at WiredTiger.wt file, if replica set members are unable to reach.At the end we drop our local database and need to reiniatalize the replica set and re-add the member to make the cluster up and running again.Do you have any other methods/process to do so.
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: How to add fields to output conditionally (dynamically) and tags: ['aggregation']

[User]
Text:Hey everyone. I have a trouble of aggregation of heterogeneous objects from the one collection. For example:My intention here is to merge objects that have the same “id” objects:This strategy works well when the query is filtered by “type” field and all objects have the same field set.When I have in the query output multiple types with different set of fields I don’t know how do I check if field exists and add it to project.
Is it possible generally with Mongo?
Code:    {
    "id": "google.com/q=blah",
    "type": "url",
    "domain": "google.com"
    "key_id": 123
    },
    {
    "id": "google.com/q=blah",
    "domain": "google.com"
    "type": "url",
    "key_id": 321
    },
    {
    "id": "google.com"
    "type": "domain",
    "key_id": 123,
    "registred": True,
    },
    {
    "id": "google.com"
    "type": "domain",
    "key_id": 321,
    "registred": True,
    }
    $group: {
         _id: "$id",
        "key_ids": {"$addToSet": "$key_id"},
        "type": {"$last": "$type"}
       "domain": {"$last": "$domain"}
    },
    $project: {
        "id": "$_id",
        "keys_ids": "$key_ids",
        "type": "$type",
       "domain": "$domain"
    }

[Assistant]
Text:Hello, @Alex_G! Welcome to the community!You can match documents with missing field like this:In the $project stage you can unify the fields set by adding a fallback value for missing fields, like this:In $project stage, if you try to include or exclude non-existent field for all documents, the pipeline will not break. So, if you want to exclude some specific field, but you are not sure if it is present in the document - don’t overthink, just exclude it You can conditionally exclude the field with $$REMOVE variable
Code:{
  $match: {
    fieldA: {
      $exists: false,
    },
  },
},
{
  $project: {
    fieldB: {
      $cond: {
        // if fieldB is not present in the document (missing)
        if: {
          $eq: [{ $type: '$fieldB' }, 'missing'],
        },
        // then set it to some fallback value
        then: 'Field is missing',
        // else return it as is
      else: '$fieldB',
      },
    },
  },
},
{
  $project: {
    unwantedFieldA: false,
    mayBeMissingFieldB: false,
  }
}
{
  $project: {
    contidionalField: {
      $cond: {
        if: {
          $eq: ['$filedA', 'abc'],
        },
        // then return its value as is
        then: '$fieldA',
        // else exclude this field
        else: '$$REMOVE',
      },
    },
  },
},

[User]
Text:Hi Slava,thanks a lot for your answer.$cond: {
if: {
$eq: [’$filedA’, ‘abc’],
},
// then return its value as is
then: ‘$fieldA’,
// else exclude this field
else: ‘$$REMOVE’,
},is exactly what I looked for.
Cheers!
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: Node.js app cannot connect to MongoDB Atlas cluster and tags: []

[User]
Text:Hi everyone.I think I have a very specific issue.
I created my first ever website with Node.js in the back-end and a MongoDB Atlas cluster as a database.I hosted my web app on Heroku where everything works fine. A few days ago when I got everything launch-ready I decided to pay for proper hosting and buy the domain I wanted.
My host server uses cPanel where it is possible to setup a Node.js app, which is what I did. However my website is not loading because I cannot establish a connection to my Atlas cluster.It’s the exact same code that works fine on Heroku, so I’m not sure it has to do with MongoDB itself. Perhaps it’s some security setting inside cPanel that prevents this connection but I was not able to find any.With the ‘useUnifiedTopology’ flag set to true I get a MongooseServerSelectionError. I saw some people having issues establishing a connection with this flag, so I tried commenting it out and now get a MongoNetworkError instead.Here’s my code:You can see a picture of my cPanel Node.js app setup and the complete callstack for these errors on my StackOverflow post here.I really hope someone can help me out. It’s my first website and the first time I host anything, so I might very well miss something but I don’t know where or what to look for anymore. I’m pretty much stuck.
Code:const connectionString = 'mongodb+srv://' + process.env.MONGODB_USER + ':' + process.env.MONGODB_PW + '@<REDACTED_APP>-y1llv.mongodb.net/<REDACTED_APP>';
mongoose
    .connect(connectionString, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useFindAndModify: false
    })
    .then(() => console.log('Database connected.'))
    .catch(err => console.log(err));

[Assistant]
Text:Hello good day!
I am currently facing the same issue. I also saw your post here on stackoverflow : https://stackoverflow.com/questions/62570924/cpanel-node-js-app-cannot-connect-to-mongodb-atlas-cluster-but-works-on-heroku.I am also using Namecheap. Have you found any solution yet?
Code:

[User]
Text:Sadly not, seems to be a very specific issue not many people know about. If nobody is able to help I’ll have to stay on Heroku for hosting and pay for Hobby dynos to use my SSL certificate. Which is annoying because I just paid for 2 years hosting on Stellar and it’s stupidly cheap compared to Heroku. Not that Hobby dynos are expensive but compared to Stellar it costs 5 times more.I’m asking MongoDB support but I’m not sure they’ll be able to help. Maybe they can give me some clues how to get more information why this MongoNetworkError occurs. But I’ll have to wait until Monday to contact them.
Code:

[Assistant]
Text:Alright thanks.
Keep me posted on when you have solved the issue.
Thanks
Code:

[Assistant]
Text:Wow I just fixed the issue.
Contact Namecheap support team and ask them to open up ports:
3000
443 and
80.Good luck!
Code:

[User]
Text:Hey, I resolved the issue!
I contacted Namecheap support via live chat, they have been very patient.
Looking at my passenger.log file and everything. Turns out they had to open the default MongoDB port 27017 on their end, no way to do it without support unfortunately.So it seems you just need to contact them and tell them you need this port opened.
Hope that’s all it takes for you as well. Good luck!
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: How to group on multiple columns and sum up individual columns and tags: ['aggregation']

[User]
Text:Hi Friends,
I am new to mongoDB and it’s really exciting.I have a scenario where i need to group on common fields(3) span across collections and have to perform summing up all the numeric columns by column wise and all string/text columns need to make “Nan” or “Null”.Help me to form the query…
attaching two sample collections, great regards from my side.The common fields across the collections are, X, Y_DT and Z. and other columns either numeric, string/text.col1771×743 15 KB col2743×752 15.2 KB
Code:

[Assistant]
Text:Hi @Murali_Muppireddy, you would want to do something similar to the following:Any non numeric value will be converted to 0 for the purposes of the $sum operation.
Code:{
    $group: {
        "_id": {
            X: "$X",
            Y_DT: "$Y_DT",
            Z: "$Z"
        },
        adj: {"$sum": "$adj"},
        bjc: {"$sum": "$bjc"},
        ...
    }
}
0$sum

[User]
Text:Thank @Doug_Duncan, I could able to get the expected results…
with the following query…But in case of bigger  collection with many number of fields, say like 500, it will be very complex to write all the filed names and sumup, is there any generic way we can write some thing like looping rest of the fields(other than grouping ones)?
Code: db.test650.aggregate(
{
    $group: {
        "_id": {
            X: "$X",
            Y_DT: "$Y_DT",
			Z: "$Z"
         },
		adj: {$sum: "$adj" },
		bjc: {$sum: "$bjc" },
		jbc: {$sum: "$jbc" },
		mnk: {$sum: "$mnk"}
    }
 }
)

[Assistant]
Text:collection with many number of fields, say like 500Seems a bit crazy is there any generic way we can write some thing like looping rest of the fields(other than grouping ones)?For the crazy request - crazy solution! And for the following documents:The aggregation will return:The solution is not ideal and can be improved, but that is another crazy story I made this just to proove, that this is possible with Mongo v4.2.
Code:db.test.aggregate([
 {
   $group: {
     // specify group-by fields here
     _id: {
       x: '$x',
       y: '$y',
       z: '$z',
     },
     docsEntries: {
       $push: {
         $objectToArray: '$$CURRENT',
       },
     },
   },
 },
 {
   $addFields: {
     docKeysList: {
       $map: {
         input: {
           $arrayElemAt: ['$docsEntries', 0],
         },
         in: '$$this.k',
       },
     },
   },
 },
 {
   $addFields: {
     // gather fields, values of which we will sum-up
     filteredKeyList: {
       $filter: {
         input: '$docKeysList',
         cond: {
           $not: {
             // specify ignored fields, that you do not want to calculate
             // at minimum, here should be _id and grouping keys
             $in: ['$$this', ['_id', 'x', 'y', 'z']],
           },
         },
       },
     },
   },
 },
 {
   $addFields: {
     // collect all entries (values of same key from all docs in the group)
     // in the single array
     groupedEntries: {
       $map: {
         input: '$filteredKeyList',
         as: 'filteredKey',
         in: {
           $reduce: {
             input: '$docsEntries',
             initialValue: [],
             in: {
               $let: {
                 vars: {
                   targetDocEntry: {
                     $filter: {
                       input: '$$this',
                       as: 'docEntry',
                       cond: {
                         $eq: ['$$docEntry.k', '$$filteredKey'],
                       },
                     },
                   },
                 },
                 in: {
                   $concatArrays: ['$$value', '$$targetDocEntry'],
                 },
               },
             },
           },
         },
       },
     },
   },
 },
 {
   $addFields: {
     calculatedEntries: {
       $map: {
         // we need to ned to return { k, v } for each key, so we can
         // transform it to single object with custom prop names
         input: '$groupedEntries',
         as: 'groupedEntry',
         in: {
           k: {
             $let: {
               vars: {
                 item: {
                   $arrayElemAt: ['$$groupedEntry', 0],
                 },
               },
               in: {
                 // here the custom prop name is calculated
                 // feel free to change the logic if needed
                 $concat: ['total_', '$$item.k'],
               },
             },
           },
           v: {
             $reduce: {
               input: '$$groupedEntry',
               initialValue: 0,
               in: {
                 $add: ['$$value', {
                   $convert: {
                     input: '$$this.v',
                     to: 'double',
                     // Change NaN to 0 for onError prop,
                     // if your props can contain various value types
                     // and you cant to calculate only number values
                     // for every document field
                     onError: NaN,
                     onNull: 0,
                   },
                 }],
               },
             },
           },
         },
       },
     },
   },
 },
 {
   $addFields: {
     results: {
       $arrayToObject: '$calculatedEntries',
     },
   },
 },
 {
   $project: {
     results: true,
   },
 },
]).pretty();
db.test.insertMany([
  { x: 1, y: 1, z: 1, propA: 5, propB: '5', propC: 't5' },
  { x: 1, y: 1, z: 2, propA: 10, propB: '10', propC: 't10' },
  { x: 1, y: 1, z: 2, propA: 15, propB: '15', propC: 't15' },
]);
[
  {
    "_id" : {
      "x" : 1,
      "y" : 1,
      "z" : 1
    },
    "results" : {
      "total_propA" : 5,
      "total_propB" : 5,
      "total_propC" : NaN
    },
  },
  {
    "_id" : {
      "x" : 1,
      "y" : 1,
      "z" : 2
    },
    "results" : {
      "total_propA" : 25,
      "total_propB" : 25,
      "total_propC" : NaN
    },
  },
]


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

[System] The following conversation has title: Can't add user after creating admin user: createUser requires authentication and tags: ['security']

[User]
Text:Anyone can help please
when I create admin user in mongo shell I got this problem
uncaught exception: Error: couldn’t add user: command createUser requires authentication :
Code:

[Assistant]
Text:You need to authenticate with the admin user to create subsequent users.
Code:

[Assistant]
Text:Hi @PT_Tadib_SejahteraIf you are trying to create your first user when authentication is enabled you need to do it using mongo on the server that mongodb is running.Or you can follow the Enable Access Control Document.
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: How can I insert null in date data type of MongoDB and tags: ['dot-net', 'compass', 'mongodb-shell']

[User]
Text:q1) pls tell how can I do it using mongo compass and also from mongo shell in date datatypefor ex we keep date in this formate , what i have to do is, i want to keep it null
and want to filter it in c# using mongo driver.
Code:{
  "dateFrom": {
        "$date": {
            "$numberLong": "1569888000000"
        }
    }
    }
}


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

[System] The following conversation has title: Beginner issue with database connect and tags: ['mongoose-odm', 'connecting']

[User]
Text:Hi,
New to Mongo environment, I am trying to build an express app with mongo using mongoose:After requiring mongoose, I use the following:""mongoose.connect(uri,{ useNewUrlParser: true, useUnifiedTopology: true },err => {});“”However, after running my app.js I get the following error:
{Error: querySrv ETIMEOUT _mongodb._tcp.cluster0-rsadq.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:197:19)
errno: ‘ETIMEOUT’,
code: ‘ETIMEOUT’,
syscall: ‘querySrv’,
hostname: ‘_mongodb._tcp.cluster0-rsadq.mongodb.net’ }Note that I already whitelisted my IP and that I use the standard uri format where I replaced test with the db nama as well as password…Thank you!
Code:console.log("mongo db connection", err);

[Assistant]
Text:querySrv ETIMEOUT _mongodb._tcp.cluster0Are you able to connect by shell?
Make sure no firewall/antivirus/vpn blocking your request
Code:

[Assistant]
Text:Hello @sam23 wellcome to the community!As Ramachandra mentioned: can you please check if you can connect via the mongo shell to make sure that you can reach the database?  In this MongoDB Doc you find further information how to connect. In case you get stuck feel free to post further information.Since you start with MongoDB and want to use Mongoose I like to point you also to this great article  Do You Need Mongoose When Developing Node.js and MongoDB Applications  from @adoCheers,
Michael
Code:

[User]
Text:Thank you for your recommandations. I will try to switch to mongo over mongoose. Concerning shell connection, as you excepected I have a problem:C:\Users\hp>mongo
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-23T16:48:50.665+0000 E  QUERY    [js] Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Aucune connexion n�a pu �tre �tablie car l�ordinateur cible l�a express�ment refus�e. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-23T16:48:50.676+0000 F  -        [main] exception: connect failed
2020-06-23T16:48:50.677+0000 E  -        [main] exiting with code 1What do you suggest for resolving this problem?
Code:

[Assistant]
Text:When you type mongo, you are trying to connect locally.Error connecting to 127.0.0.1:27017Since your cluster is cluster0-rsadq.mongodb.net, you should tryCheck the usage on the first link provided by @michael_hoeller, to see how to supply your credentials.
Code:mongo 'mongodb+srv://cluster0-rsadq.mongodb.net'

[User]
Text:Finally, I get this error:DNSHostNotFound: Failed to look up service “”:Cette op├®ration sÔÇÖest termin├®e car le d├®lai dÔÇÖattente a expir├®.
try ‘C:\mongodb\bin\mongo.exe --help’ for more information
Code:

[User]
Text:I followed the docs but this time I get this :C:\Users\hp>mongo ‘mongodb+srv://cluster0-rsadq.mongodb.net’ -u my_username -p password
MongoDB shell version v4.2.6
2020-06-23T20:52:01.514+0000 F  -        [main] exception: No digits
2020-06-23T20:52:01.516+0000 E  -        [main] exiting with code 1Note that my_username and password were replaced and I get the same log
Code:

[Assistant]
Text:Use normal double or single quotes.  Yours are new web version.
Code:

[User]
Text:That didn’t change anything, I keep getting an error related  to: DNSHostNotFound: Failed to look up service
Code:

[Assistant]
Text:But your earlier post gives different error with SRV string(no digits)Did your try with doble quotes as Steeve suggested?or create a test user and share pwd for us to checkI tried your string with some id/pwd
It gave authentication erroror try long form of connect string which has names of all 3 nodes of your cluster with replicaset name
It is possible your are using a DNS resolver that does not support DNS seedlist
Also make sure no vpn/firewall,anti virus issues blocking your connection
Code:

[Assistant]
Text:Hello @sam23the connection string seems to be ok, quote or double quote should not matter. I can reach your DB, but not authenticate since I used my_username / passwordAs Ramachandra noted, this seems to be more of an infrastructure problem.It is possible your are using a DNS resolver that does not support DNS seedlist
Also make sure no vpn/firewall,anti virus issues blocking your connection
Code:mi01@LU30:~$ mongo 'mongodb+srv://cluster0-rsadq.mongodb.net' -u my_username -p password
MongoDB shell version v3.6.8
connecting to: mongodb+srv://cluster0-rsadq.mongodb.net
2020-06-24T07:05:20.706+0200 I NETWORK  [thread1] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-02-rsadq.mongodb.net.:27017,cluster0-shard-00-01-rsadq.mongodb.net.:27017,cluster0-shard-00-00-rsadq.mongodb.net.:27017
2020-06-24T07:05:20.793+0200 I NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-00-rsadq.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-00-rsadq.mongodb.net.:27017 with a 5 second timeout)
2020-06-24T07:05:20.793+0200 I NETWORK  [thread1] Successfully connected to cluster0-shard-00-02-rsadq.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-02-rsadq.mongodb.net.:27017 with a 5 second timeout)
2020-06-24T07:05:20.813+0200 I NETWORK  [thread1] changing hosts to Cluster0-shard-0/cluster0-shard-00-00-rsadq.mongodb.net:27017,cluster0-shard-00-01-rsadq.mongodb.net:27017,cluster0-shard-00-02-rsadq.mongodb.net:27017 from Cluster0-shard-0/cluster0-shard-00-00-rsadq.mongodb.net.:27017,cluster0-shard-00-01-rsadq.mongodb.net.:27017,cluster0-shard-00-02-rsadq.mongodb.net.:27017
2020-06-24T07:05:20.880+0200 I NETWORK  [thread1] Successfully connected to cluster0-shard-00-00-rsadq.mongodb.net:27017 (1 connections now open to cluster0-shard-00-00-rsadq.mongodb.net:27017 with a 5 second timeout)
2020-06-24T07:05:20.887+0200 I NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-02-rsadq.mongodb.net:27017 (1 connections now open to cluster0-shard-00-02-rsadq.mongodb.net:27017 with a 5 second timeout)
2020-06-24T07:05:20.965+0200 I NETWORK  [thread1] Successfully connected to cluster0-shard-00-01-rsadq.mongodb.net:27017 (1 connections now open to cluster0-shard-00-01-rsadq.mongodb.net:27017 with a 5 second timeout)
Implicit session: session { "id" : UUID("55b56a74-945f-47d9-923d-dabe2ab99112") }
MongoDB server version: 4.2.8
WARNING: shell and server versions do not match
2020-06-24T07:05:21.141+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-02-rsadq.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node cluster0-shard-00-02-rsadq.mongodb.net:27017: Authentication failed.
2020-06-24T07:05:21.179+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-00-rsadq.mongodb.net:27017 as failed :: caused by :: Location11002: can't authenticate against replica set node cluster0-shard-00-00-rsadq.mongodb.net:27017: socket exception [CONNECT_ERROR] for cluster0-shard-00-00-rsadq.mongodb.net:27017
2020-06-24T07:05:21.216+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-01-rsadq.mongodb.net:27017 as failed :: caused by :: Location11002: can't authenticate against replica set node cluster0-shard-00-01-rsadq.mongodb.net:27017: socket exception [CONNECT_ERROR] for cluster0-shard-00-01-rsadq.mongodb.net:27017
2020-06-24T07:05:21.259+0200 I NETWORK  [thread1] Marking host cluster0-shard-00-02-rsadq.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node cluster0-shard-00-02-rsadq.mongodb.net:27017: Authentication failed.
2020-06-24T07:05:21.259+0200 E QUERY    [thread1] Error: can't authenticate against replica set node cluster0-shard-00-02-rsadq.mongodb.net:27017: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20
@(auth):6:1
@(auth):1:2
exception: login failed
mi01@LU30:~$

[Assistant]
Text:You could try using google’s DNS servers at 8.8.8.8 and 8.8.4.4.
Code:

[User]
Text:I think I am close to establishing the connection: I verified whitelisted IPs, removed antivirus but this time:connecting to: mongodb://cluster0-rsadq.mongodb.net:27017/?compressors=disabled&gssapiServiceName=mongodb*** It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.2020-06-24T15:45:21.281+0000 E  QUERY    [js] Error: couldn’t connect to server cluster0-rsadq.mongodb.net:27017, connection attempt failed: HostNotFound: Could not find address for cluster0-rsadq.mongodb.net:27017: SocketException: H�te inconnu. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-24T15:45:21.296+0000 F  -        [main] exception: connect failed
2020-06-24T15:45:21.296+0000 E  -        [main] exiting with code 1I get a Host Not Found error!By the way thank you for your indications, I learned a lot of concepts by looking up these issues.
Code:

[Assistant]
Text:The URI you are using is wrong.  You are using part old style and part SRV.  The DNS name cluster9-rsadq.mongodb.net does not correspond to a host.  It correspond to a cluster.  It has the following DNS entry:So your hosts are:To use SRV, remove the port number and use mongodb+srv:// rather than mongodb:// like it was given in the examples.
Code:;; ANSWER SECTION:
cluster0-rsadq.mongodb.net. 59  IN      TXT     "authSource=admin&replicaSet=Cluster0-shard-0"
;; ANSWER SECTION:
_mongodb._tcp.cluster0-rsadq.mongodb.net. 59 IN SRV 0 0 27017 cluster0-shard-00-00-rsadq.mongodb.net.
_mongodb._tcp.cluster0-rsadq.mongodb.net. 59 IN SRV 0 0 27017 cluster0-shard-00-01-rsadq.mongodb.net.
_mongodb._tcp.cluster0-rsadq.mongodb.net. 59 IN SRV 0 0 27017 cluster0-shard-00-02-rsadq.mongodb.net.


[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 transform a SQL self join to a MongoDB query and tags: []

[User]
Text:Hi everybody!
I want to show you a problem:
in RDBMS i would like to do this querySELECT PERS_ID FROM table WHERE type=‘A’ and PERS_ID not in (SELECT PERS_ID from TABLE where type=‘B’)how can i do this query in mongo?I have
{pers_id: “1”, type: “A”}
{pers_id: “2”, type: “A”}
{pers_id: “2”, type: “B”}
{pers_id: “3”, type: “A”}
{pers_id: “4”, type: “B”}
and i want to see as result{pers_id: “1”}
{pers_id: “3”}How can i do??
Code:

[Assistant]
Text:Hello, @Annsa_Lisa, welcome to the community!
You can achieve the desired result with .find() commandOr using .aggregate() command:
Code:db.your_collection.find({ type: 'A' }, { _id: false, pers_id: true });
db.your_collection.aggregate([
  {
     $match: {
        type: 'A',
     },
  },
  {
     $project {
        _id: false,
        pers_id: true,
     },
  },
]);

[User]
Text:I don’t think that is what I want…
I want to show collections where the pers_id with type A doesn’t have any other document of type B with the same pers_id.
Can you help me?
Thanks
Code:

[Assistant]
Text:I would $group by pers_id using $addToSet the type in a types array and then $match out documents that have type B in the types array.
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: Checking if a field contains a string and tags: []

[User]
Text:Hi, I need to know how to get all documents which field contains specified string in JAVA.
I know in Mongo shell it look like this: ```
db.users.findOne({“username” : /.son./i});
Code:So, can anyone translate it to java ? ;)

[Assistant]
Text:With MongoDB Java driver use the find method - see MongoDB Java Find filter Examples. To build a regex query use the find filter Filters#regex method.The code can be like this:
Code:String patternStr = ".son.";
Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
Bson filter = Filters.regex("username", pattern);
collection.find(filter);

[User]
Text:I did it just like u did, It works most the time but when I have string in DB for example “example string” and I try to look for “string” it will not find anything. Probably space  mess it up.
Code:

[Assistant]
Text:It is more about Regular Expressions (not Java programming). The character . (dot) essentially  matches any character .  To make the dot optional append a ? . The question mark makes the preceding token in the regular expression optional.String patternStr = ".son.?";
Code:.?String patternStr = ".son.?";


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

[System] The following conversation has title: Nested lookup 'join' using .net driver and tags: ['dot-net']

[User]
Text:Hello,
i want to know how to do complicated nested lookup in c#see this code for example:
Inbox.cshow to perform nested lookup or join with one call to return an array of Inboxes with its Rules and the Rules other objects such as Service and WorkOrderStatus
Code:public class Inbox : TDocument
{
    public string Name {get;set;}
    public List<MongoDBRef> WorkOrdersRef {get;set;}
    public List<InboxRule> Rules {get;set;}

    [BsonIgnore] public List<WorkOrder> WorkOrders {get;set;}
}

InboxRule.cs

public InboxRule : TDocument
{
    public MongoDBRef ServiceRef {get;set;}
    public MongoDBRef WorkOrderStatusRef {get;set;}

    [BsonIgnore] public Service Service {get;set;}
    [BsonIgnore] public WorkOrderStatus WorkOrderStatus {get;set;}
}

[Assistant]
Text:Hi @Moataz_Al-HANASH , welcome!how to perform nested lookup or join with one call to return an array of Inboxes with its Rules and the Rules other objects such as Service and WorkOrderStatusBased on the class mapping, it looks like Rules is an embedded list inside of Inbox. You should be able to query any Inbox or multiple Inbox and be able to retrieve the Rules as well.Also you may find $lookup aggregation stage a useful resource.If you still have further questions, it would be useful to provide:Note: I noticed that you have MongoDBRef as type, if that refers to DBRefs  depending on the use case you may find a manual reference of _id would be easier to use. See also database references.Regards,
Wan.
Code:RulesInboxInboxInboxMongoDBRef_id

[User]
Text:furthermore :A)How to perform  Join  using  LINQ or ‘Aggregation’ to fill the property  WorkOrders  based on the references in the property  WorkOrdersRefB)How to perform  Join  using  LINQ or ‘Aggregation’  to fill the properties of the embedded document  InboxRule  known as  Service  and  WorkOrderStatusOr simply, how to populate the  [BsonIgnore] -properties for an  Inbox  object and it’s embedded document property known as  Rules
Code:JoinLINQWorkOrdersWorkOrdersRefJoinLINQInboxRuleServiceWorkOrderStatus[BsonIgnore]InboxRules

[User]
Text:Thanks for our replay.that is right , Rules are embedded inside of Inbox object
but if you notice Service and WorkOrderStatus are NOT embedded inside Rules ,  i want to retrieve an Inbox object joined with WorkOrder and the embedded Rules should also be joined with Service and WorkOrderStatus and all mapped back to Inbox objectso basiclly im looking for something like this:i tried this in C# to get the results needed but failed:but the problem is the above code does not return list of Inbox , it returns list of InboxRulehow to perform nested lookup and map results to Inbox object ?Iam using 2.10.4 MongoDB .NET/C# driver
also using Atlas 4.2.6 as my MongoDb serverThanks in advance
Code:RulesInboxServiceWorkOrderStatusRulesInboxWorkOrderRulesServiceWorkOrderStatusInboxInbox {
   WorkOrders : [
               {
                   WorkOrderId : "123",
                    ...
               }
               {
                   WorkOrderId : "445",
                    ...
               }
    ],
   Rules : [
                         {
                            Service : {
                                      Name : "Service 1"
                                           } ,
                            WorkOrderStatus : {
                                      Name : "Status 1"
                                          }
                         }
                         {
                            Service : {
                                      Name : "Service 2"
                                           } ,
                            WorkOrderStatus : {
                                      Name : "Status 2"
                                          }
                         }
    ]
}
_dbContext.DbSet<Inbox>().Aggregate()
                     .Lookup("WorkOrder", inboxKeyToWorkOrders, woKey, navProp) // to fill WorkOrders property in Inbox
                     .Unwind(a => a.Rules, new AggregateUnwindOptions<InboxRule>() { PreserveNullAndEmptyArrays = true })
                     .Lookup("Service", inboxRuleKeyToService, serviceId, inboxRuleNavPropService) // to fill Service property in InboxRule
                     .Lookup("WorkOrderStatus", inboxRuleKeyToStatus, statusId, inboxRuleNavPropStatus) // to fill WorkOrderStatus property in InboxRule
                     .ToList();
Inbox

[User]
Text:@wan
i added a replay sir
Code:

[Assistant]
Text:Hi @Moataz_Al-HANASH ,Based on your C# code snippet, there are 4 collections involved (Inbox, WorkOrder, Service, and WorkOrderStatus), and you’re trying to perform multiple lookups to combine them.Could you clarify the question by providing example documents for the collections ?
Also, what does the current output document that you’re getting ?Regards,
Wan.
Code:InboxWorkOrderServiceWorkOrderStatus

[User]
Text:@wanThis is the example documentsInbox DocumentWorkOrder document:WorkOrderStatusas for second question , i dont get a result yet
Code:    {
              _id : ObjectId('3213asdaadda'),
              Name : 'InboxName',
              WorkOrdersRef : [
                              0:ObjectId('1'),
                              1:ObjectId('2'),
                              2:ObjectId('3'),
                          ],
              Rules : [
                     {
                          ServiceRef: ObjectId('2134'),
                          WorkOrderStatusRef: ObjectId('213asd4'),
                     },
                     {
                          ServiceRef: ObjectId('21341523'),
                          WorkOrderStatusRef: ObjectId('2112131134'),
                     }
                  ]
    }
    {
         _id:Object('1'),
         ... *some other not related to the question fields and arrays*
    }
    {
         _id:Object('2'),
         ... *some other not related to the question fields and arrays*
    }
    {
         _id:Object('3'),
         ... *some other not related to the question fields and arrays*
    }
    {
           _id : ObjectId('12313'),
           Name : 'Initiated'
    },
{
       _id : ObjectId('12313'),
       Name : 'Closed'
}

[Assistant]
Text:Hi @Moataz_Al-HANASH,Given the example document for inbox, workorder, and workorderstatus collections, you could perform an aggregation as below example:how to populate the [BsonIgnore] -properties for an Inbox object and it’s embedded document property known as RulesThe use of Lookup and Unwind will change the class shape, I’d recommend to either define a new class that matches the result/output type, or just use BsonDocument .Regards,
Wan.
Code:inboxworkorderworkorderstatusvar collection = database.GetCollection<Inbox>("inbox");
var docs = collection.Aggregate()
                     .Lookup("workorder", "WorkOrdersRef", "_id", "WorkOrders")
                     .Unwind("Rules")
                     .Lookup("workorderstatus", "Rules.WorkOrderStatusRef", "_id", "Rules.WorkOrderStatus")
                     .ToList();
[BsonIgnore]InboxRulesLookupUnwindBsonDocument


[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 install mongodb from source code and tags: ['server']

[User]
Text:Hello All,Hope you’re doing good…I would like to know how to build mongodb with source code.  We have a requirement to install mongodb from source code instead of RPM’s .  I downloaded source code from Try MongoDB Atlas Products | MongoDBPlease help me out in this regard.Thanks,
Satya
Code:

[Assistant]
Text:Hi @satya_dommeti and welcome to the community!I would recommend taking a look at the Building MongoDB docs from the public MongoDB GitHub repo.Best of luck.
Code:

[User]
Text:Hi  @Doug_Duncan  Thank you for your response.  The document you shared is very helpful.I am trying build mongodb from source code but getting the below error.  It would be great if you help me in this regard.scons: Reading SConscript files …
scons: running with args /usr/local/bin/python3.8 buildscripts/scons.py install-mongod
Invalid MONGO_VERSION ‘’, or could not derive from version.json or git metadata. Please add a conforming MONGO_VERSION=x.y.z[-extra] as an argument to SConsOS version :
Red Hat Enterprise Linux Server release 7.6 (Maipo)Thanks,
Satya
Code:

[Assistant]
Text:Hi Satya,I tried to compile mongod using the documentation pointed at by @Doug_Duncan on my Mac, and it seems straightforward. Here’s exactly what I did:Seems to be working just fine. One caveat is to check out the build instructions for the version you wanted to build, since for version 4.2.8 that I tried, the build target is called  mongod. For the master branch, it’s called install-mongod.The error you posted: could not derive from version.json or git metadata seems to indicate that you did not do a git clone of the mongo repository, so it doesn’t know which version to build. You might want to check if the step-by-step method using git clone like I did above works first before trying anything else, to confirm that you have the required tools.Best regards,
Kevin
Code:mongod$ git clone https://github.com/mongodb/mongo.git

$ cd mongo

$ git checkout r4.2.8

$ pip3 install -r etc/pip/compile-requirements.txt

$ python3 buildscripts/scons.py mongod
... 25 minutes later ...
Install file: "build/opt/mongo/mongod" as "mongod"
scons: done building targets.

$ build/opt/mongo/mongod --version
db version v4.2.8
git version: 43d25964249164d76d5e04dd6cf38f6111e21f5f
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64
mongodinstall-mongodcould not derive from version.json or git metadatagit clone

[Assistant]
Text:I would like to know how to build mongodb with source code. We have a requirement to install mongodb from source code instead of RPM’s .Hi Satya,Please provide some more detail on your requirements:What version of MongoDB Server (x.y.z) are you trying to build?Build requirements and instructions may vary between major server releases, so make sure you are following relevant instructions and have met all of the prerequisites (versions of C++ compiler, Python, and SCons are especially important).What is your motivation for building from source?If you aren’t modifying source code, there may be an alternative approach to suggest (for example, offline installation of RPMs for an airgapped server). If you are trying to build a version of MongoDB server that doesn’t have prebuilt binaries for RHEL 7, that would also be helpful context.Regards,
Stennie
Code:x.y.z

[User]
Text:Hi @Stennie_X Thanks for your reply.  please find my inline commentsWhat version of MongoDB Server ( x.y.z ) are you trying to build?I’m trying to build MongoDB server 4.2.3.  The error which I posted is resolved after adding a parameter MONGO_VERSION=4.2.3 as below.But it is refusing to build with compiler that does not meet requirement.It is showing that GCC 8.2 or newer is required to build MongoDB. But I already installed GCC 8.3 versionI tried below command also but no luckWhat is your motivation for building from source?This is my client requirement to provide security.  Client don’t want to install mongodb with RPM’sRegards,
Satya
Code:x.y.z$ python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3
$ python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3
scons: Reading SConscript files ...
scons: running with args /usr/local/bin/python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3
scons version: 3.0.4
python version: 3 8 3 'final' 0
CC is gcc
gcc found in $PATH at /opt/rh/devtoolset-8/root/usr/bin/gcc
CXX is g++
g++ found in $PATH at /opt/rh/devtoolset-8/root/usr/bin/g++
Checking whether the C compiler works... yes
Checking whether the C++ compiler works... yes
Checking that the C++ compiler can link a C++ program... yes
Checking if C++ compiler "g++" is GCC... yes
Checking if C compiler "gcc" is GCC... yes
Detected a x86_64 processor
Checking if target OS linux is supported by the toolchain... yes
**Checking if C compiler is GCC 8.2 or newer...no**
**Checking if C++ compiler is GCC 8.2 or newer...no**
ERROR: Refusing to build with compiler that does not meet requirements
See /home/mongoadm/mongo-r4.2.3/build/scons/config.log for details
$ tail  /home/mongoadm/mongo-r4.2.3/build/scons/config.log
  |int main(int argc, char* argv[]) {
  |    return 0;
  |}
  |
Compiling build/scons/opt/sconf_temp/conftest_12.o
build/scons/opt/sconf_temp/conftest_12.cpp:7:2: error: #error GCC 8.2 or newer is required to build MongoDB
 #error GCC 8.2 or newer is required to build MongoDB
  ^
scons: Configure: no
$/opt/rh/devtoolset-8/root/usr/bin/gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)

$/opt/rh/devtoolset-8/root/usr/bin/g++ --version
g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)

cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.6 (Maipo)
python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3     --CC=/opt/rh/devtoolset-8/root/usr/bin/gcc --CXX=/opt/rh/devtoolset-8/root/usr/bin/g++ --disable-warnings-as-errors

[Assistant]
Text:Hi Satya,Please make sure you are following the 4.2 version of Building MongoDB instructions (which should be docs/building.md in your source checkout).The SCons install-mongod target used in your output is for a newer server release branch (4.4 or master) and should be mongod for 4.2.It looks like you have an appropriate version of gcc/g++ installed, but perhaps there are conflicting versions in your path.–CC=/opt/rh/devtoolset-8/root/usr/bin/gcc --CXX=/opt/rh/devtoolset-8/root/usr/bin/g++These options should be set as environment variables, not as parameters. Try including these in your SCons command line without the leading --:CC=/opt/rh/devtoolset-8/root/usr/bin/gcc CXX=/opt/rh/devtoolset-8/root/usr/bin/g++This is my client requirement to provide security.I noticed you are building MongoDB 4.2.3. The latest 4.2 release is currently 4.2.8 (see MongoDB 4.2 Minor Releases). I strongly recommend staying current with minor releases for the latest security and stability fixes.If your client is concerned about security and stability, the official packages should provide more assurance on that front than building from source. All builds are extensively tested via our public (and open source) Evergreen CI and published packages are signed for verification.If you build from source, there may be variations in your build environment that cause unexpected issues.Regards,
Stennie
Code:docs/building.mdinstall-mongodmastermongodgccg++--


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

[System] The following conversation has title: Replica set with 3 DB Nodes and 1 Arbiter and tags: ['replication']

[User]
Text:Hi,I have created a replicaset of 3 database nodes and one arbiter.
Where 2 database nodes in Production environment and DR arbiter and another secondary DB node. Arbiter also holds a vote in my configuration,
As if one node fails,
in order to promote one secondary as a primary, there must be at least 3 votes.
Is my configuration is wrong?
4 voting members including arbiter is wrong or correct?
Please help?
Code:

[Assistant]
Text:Hello @Kasun_Magedaragama  ,
and welcome to the community. We’re glad to have you join us and we look forward to your contributions.4 voting members including arbiter is wrong or correct?An even number of voting members is always a bad choice for elections.  You generally add a  cheap (non dataholding) arbiter to archive an odd number of voting members - you can look at an arbiter as an tie-breaker.  The MongoDB Documentation provides further detail–
Beside this you should also keep the MongoDB write concern in mind.A write concern of zero means that the application doesn’t wait for any acknowledgments. The write might succeed or fail. The application doesn’t really care. It only checks that it can connect to the node successfully.The default write concern is one . That means the application waits for an acknowledgment from a single member of the replica set, specifically, the primary. This is a baseline guarantee of success.Write concerns greater than one increase the number of acknowledgments to include one or more secondary members.  Higher levels of write concern correspond to a stronger guarantee of write durability.Majority is a keyword that translates to a majority of replica set members. Divide the number of members by two and round up. So this three-member replica set has a majority of two. A five-member replica set would have a majority of three-- so on and so forth. The nice thing with majority is that you don’t have to update your write concern if you increase the size of your replica set.Hope this helps
Michael
Code:MongoDB write concernwrite concern

[User]
Text:Dear Michael,Thanks a lot for you response. What we thought is, if one DB node goes down,
In order to elect a Primary, there should be remaining 3 nodes right?
That means 2 DB nodes + Arbiter?Correct me if im wrong
Code:

[Assistant]
Text:Hello @Kasun_Magedaragamasorry for the late response, unfortunately work kept me busy.I struggle with a clear answer as well. If your primary member goes down when running in a standard configuration (odd number of hosts) then you run into the risk of a tie when voting. Maybe @Stennie_X can add more information why an odd number is the recommended setup.My understanding of the voting process is the following:The primary node is the first point of contact for any client communicating to the database. Even if secondaries go down, the client will continue communicating with the node acting as primary until the primary is unavailable.Elections take place whenever there’s a change in topology. Reconfiguring a replica set will always trigger an election that may or may not elect a new primary. But you will definitely see a new primary elected in two cases:The method to figure out which secondary will run for election begins with priority and whichever node has the latest copy of the data. Let’s say every node in your set has the same priority, which is the default. And this node has the latest copy of the data. So it’s going to run for election, and then automatically vote for itself. Then it’s going to ask the other two node(s) for support in the election. The response should be: you have a pretty recent copy of the data, you seem like a good candidate. Then they’ll pledge their support as well. This node will be elected primary.There is also the very slim possibility that two nodes run for election simultaneously. But in a replica set with an odd number of nodes, this doesn’t matter.These two nodes are both going to run, which means they’re both going to vote for themselves. And then this node is going to essentially decide which one of these nodes becomes primary by virtue of a tiebreaker.
This becomes a problem when we have an even number of voting members in a set.If two secondaries are running for election simultaneously and there are an even number of remaining nodes in the set, there’s a possibility that they split the vote and there’s a tie. Now a tie is not the end of the world, because the nodes will just start over and hold another election. The problem with repeating elections over and over is that any applications accessing the data will have to pause all activity and wait until a primary is elected.  An even number of nodes increases the chances an election has to be repeated, so we generally try to keep an odd number in our replica sets.Another important aspect of elections is the priority assigned to each node in a set. Priority is essentially the likelihood that a node will become the primary during an election. The default primary for a node is 1, and any node with priority 1 or higher can be elected primary. You can increase the priority of a node if you want it to be more likely at this node becomes primary. But changing this value alone does not guarantee that.
You can also set the priority of node to be 0 if you never want that node to become primary. A priority 0 node can still vote in elections, but it can’t run for election.Michael
Code:

[Assistant]
Text:To elect a primary a majority of voting members must vote for the candidate.I believe majority is expressed as floor(voting_members_in_cluster / 2 ) +1. ( I cannot find a reference for this in the documentation right now)In the case an even number sized cluster is split(with 2 nodes partitioned on each side) it is impossible to gain a majority and hence no candidate elected for primary.
Code:floor(voting_members_in_cluster / 2 ) +1

[Assistant]
Text:HiI am aware of that rule but in a slightly different context:MongoDB write concern is an acknowledgment mechanism that developers can add to write operations.
Higher levels of acknowledgment produce a stronger durability guarantee. Durability means that the write has propagated to the number of replica set member nodes specified in the write concern.Majority here is defined as a simple majority of replica set members. So divide by two, and round up.
Taken from M103: Basic Cluster AdministrationI was under the same assumption as you. But I found nowhere a statement that an election is blocked with an even number of members and I doubt that it would.Michael
Code:

[Assistant]
Text:In the case an even number sized cluster is split(with 2 nodes partitioned on each side) it is impossible to gain a majority and hence no candidate elected for primary.@chris you are correct. If you have a four node replicaset with two nodes each in two different data centers and there is a network partition making it so you cannot get a majority vote count, then you will be left with no PRIMARY server and four SECONDARY servers. This is why it is recommended to have an odd number of voting members with a majority of nodes in the PRIMARY data center, or even better, spread across three or more data centers if possible.
Code:

[Assistant]
Text:Welcome to the community @Kasun_Magedaragama! 4 voting members including arbiter is wrong or correct? As @michael_hoeller mentioned, an odd number of voting members is recommended. The addition of an arbiter to your 3 member deployment adds operational risk without providing any benefit.Primaries are elected (and sustained) based on a consensus vote from a strict majority (>50%) of configured voting members. The strict majority requirement is to avoid situations that might otherwise allow more than one primary (for example, a network partition separating voting members equally between data centres).If you add additional members to a replica set, there should be some motivation such as increasing data redundancy or improving fault tolerance. Adding an arbiter to a 3 member replica set does not contribute to either of those aspects.With 3 voting members, the strict majority required to elect a primary is 2 votes which means there is a fault tolerance of 1 member that can be unavailable. If a majority of voting members aren’t available, a primary cannot be elected (or sustained) and all data-bearing members will transition to SECONDARY state.With 4 voting members, the strict majority required to elect a primary is 3 votes which means there is still a fault tolerance of 1 despite the added member. There is also no improvement in data redundancy, since an arbiter only participates in elections.However, if the 4th member is an arbiter this introduces some potential operational complications when the replica set is running in a degraded state (elected primary with one data-bearing member unavailable):An arbiter contributes to the voting majority for a replica set election but cannot contribute to acknowledgement of write operations (since an arbiter doesn’t write any data).If you want to avoid potential rollback of replica set writes, a  majority write concern is recommended. However, a majority write concern cannot be acknowledged if your replica set currently only has a voting majority (using an arbiter) rather than a write majority. Operations with majority write concern will either block indefinitely (default behaviour) or time out (if you have specified the wtimeout option).Cache pressure will be increased because more data will be pinned in cache waiting for the majority commit point to advance. Depending on your workload, this can cause significant problems if your replica set is in degraded state. There is a startup warning for Primary-Secondary-Arbiter (PSA) deployments which also would apply to your PSSA scenario in MongoDB 4.4 and earlier: Disable Read Concern Majority. For MongoDB 5.0+, please see Mitigate Performance Issues with PSA Replica Set.Typically, no.An arbiter can still be useful if you understand the operational caveats and are willing to compromise robustness for cost savings. You generally add a cheap (non dataholding) arbiter to archive an odd number of voting members - you can look at an arbiter as an tie-breaker. Considering an arbiter as a tie-breaker is the best possible interpretation, but adding an arbiter does not have the same benefits as a secondary.Where possible I would strongly favour using a data-bearing secondary over an arbiter for a more robust production deployment. What we thought is, if one DB node goes down, In order to elect a Primary, there should be remaining 3 nodes right? The required voting majority is based on the configured number of replica set members, not on the number that are currently healthy. The voting majority for a replica set with 4 voting members is always 3 votes.Think of replication as analogous to RAID storage: your configuration determines the level of data redundancy, performance, and fault tolerance. If there is an issue with availability of one (or more) of your replica set members, the replica set will run in a degraded mode which allows continued write availability (assuming you still have a quorum of healthy voting members to sustain a primary) and read availability (as long as at least one data-bearing member is online). If your primary member goes down when running in a standard configuration (odd number of hosts) then you run into the risk of a tie when voting. Explanations around replica set configuration are often reductive to try to provide more straightforward understanding.The election algorithm can handle an even number of voting members (for example, this is the scenario when you have a 3 member replica set with 1 member down). There are other factors that influence elections including member priority and freshness, so repeated tie-breaking scenarios should not be a concern. However, you generally want to remove any potential speed bumps for your normal deployment state (“all members healthy”). A configuration with an odd-number of voting members is also easier to rationalise when considering scenarios for data redundancy and fault tolerance.MongoDB’s historical v0 election protocol (default in MongoDB 3.0 and earlier) only supported one election at a time, so any potential ties had a more significant impact on elapsed time to reach consensus. The modern v1 election protocol (default in MongoDB 3.2+) supports multiple concurrent elections for faster consensus. If you want to learn more, there’s a relevant talk from MongoDB World 2015: Distributed Consensus in MongoDB. I believe majority is expressed as floor(voting_members_in_cluster / 2 ) +1 . ( I cannot find a reference for this in the documentation right now) That is the correct determination for voting majority, but write majority is based on data-bearing members. Ideally those calculations should be the same for a deployment, but arbiters and members with special configuration (eg delayed secondaries) will have consequences for write acknowledgements.In MongoDB 4.2.1+, the rs.status() output now has explicit  majorityVoteCount  and  writeMajorityCount calculations to remove any uncertainty.Regards,
Stennie
Code:SECONDARYmajoritywtimeoutfloor(voting_members_in_cluster / 2 ) +1rs.status()majorityVoteCountwriteMajorityCount


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

[System] The following conversation has title: Query document with string and boolean value and tags: ['queries']

[User]
Text:Hi, I need to apply 2 filters  for quering data from my db - by String field and boolean field.
for example I want to get 20 documents with with fields: boolean: true  string:“someString” how I can achieve that ?
Code:

[Assistant]
Text:You can use the $and operator.
Code:

[Assistant]
Text:There is no real need to break out the $and as it is implicit.db.collection.find({boolean: true, string:"someString"}).limit(20)
Code:db.collection.find({boolean: true, string:"someString"}).limit(20)

[Assistant]
Text:Hi @Zdziszkee_N_A,Please see Query Documents in the MongoDB manual for examples of queries in the mongo shell, Compass, and drivers.The relevant example for your question is Specify AND Conditions:db.foo.find( { boolean: true, string: ‘someString’}).limit(20)You can use the $and operator.A compound query is implicitly an AND query. You only need to use $and when specifying multiple conditions on the same field name (otherwise repeated field names would overwrite each other in the query object in most languages):For example, try this in a mongo shell or node interpreter:foo = {string: ‘someString’, string: ‘someOther’}In JavaScript the last value of a duplicated property will be the only value set:{ “string” : “someOther” }Regards,
Stennie
Code:mongoANDAND$andmongonode


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

[System] The following conversation has title: Nested arrays how to do query and update? and tags: ['aggregation']

[User]
Text:Hi Team,I have a collection with 5000k documents with multiple nested arrays.I want to update the prodId key from old to new value.Some of the documents have only 1 prodId value and other documents have one or more multiple prodId values and few of the documents have no prodId value.output:output:I feel the 2nd method of querying is correct for this type of nested arrays. Correct me if I am wrong and based on this how to update one single prodId in all the documents where that prodId is available?for example how to update prodId value from VU0074 to AC0067 available in all the 5000k documents?Regards
Mam
Code:db.test01.aggregate([{$unwind:"$ordDoc.custOrderItems"},
   {$match:{"ordDoc.custOrderItems.custOrdSubItems.prodId":"VU0074"}},
   {$project:{"ordDoc.custOrderItems.custOrdSubItems.prodId":1}}]).pretty()
{
	"_id" : "ORN-178450914676",
{
   "ordDoc":{
      "custOrdItems":{
         "custOrdSubItems":[
            {
               "prodId":"VU0091"
            },
            {
               "prodId":"VU0074"
            },
            {
               "prodId":"VU0081"
            },
            {
               "prodId":"VU0033"
            },
           {
               "prodId": " "
            },
            {
               "prodId":"VU0038"
            }
         ]
      }
   }
}
db.test01.aggregate([{$unwind:"$ordDoc.custOrderItems"},{$unwind:"$ordDoc.custOrderItems.custOrderSubItems"},{$match:{"ordDoc.custOrderItems.custOrderSubItems.prodId":"VU0074"}},{$project:{"ordDoc.custOrderItems.custOrderSubItems.prodtId":1}}]).pretty()
{
	"_id" : "ORN-12345678900096",
	"ordDocument" : {
		"custOrderItems" : {
			"custOrderSubItems" : {
				"prodId" : "VU0074""
			}
		}
	}
},

[Assistant]
Text:Welcome to the community @Mamatha_M !There are 2-3 errors on your queries.But infact you don’t need the Aggregation Framework for a problem like this. You rather need the updateMany() command. We are typically requesting a data schema with an attribute pattern.I simplified your work, I found the solution. First, you didn’t specify if in your “custOrdSubItems” array there could be several sub-documents with the same value for the “prodId” field. So there are actually 2 solutions.db.TestCollection.updateMany({“ordDoc.custOrdItems.custOrdSubItems.prodId”: “VU0074”},
{$set: {“ordDoc.custOrdItems.custOrdSubItems.$.prodId”: “newValue”}})db.TestCollection.updateMany({},
{$set: {“ordDoc.custOrdItems.custOrdSubItems.$[element].prodId”: “newValue”}},
{arrayFilters: [{“element.prodId”: “VU0074”}],multi: true})More elegant than an aggregation query, right ? If you have any questions don’t hesitate.
Code:

[Assistant]
Text:for example how to update prodId value from VU0074 to AC0067 available in all the 5000k documents?The solution 2 by @Gaetan_MORLET  works fine.If you are sure , really sure , that there are no duplicates as explained above you can use the $(update) operator. If there are duplicates in the array it will just update the first subdocument.This is about the solution 1. This will not work. The $(update) operator  cannot be applied for nested arrays. From the documentation:Nested Arrays
The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value
Code:$(update)

[User]
Text:db.TestCollection.updateMany({“ordDoc.custOrdItems.custOrdSubItems.prodId”: “VU0074”},
{set: {“ordDoc.custOrdItems.custOrdSubItems…prodId”: “newValue”}})I will try the 2nd query.Also can  i get the find query for both these options?
Code:

[Assistant]
Text:Correction about the update operation:db.TestCollection.updateMany({},
{set: {“ordDoc.custOrdItems.custOrdSubItems.[element].prodId”: “newValue”}},
{arrayFilters: [{“element.prodId”: “VU0074”}],multi: true})I did miss something in the update solution 2 by @Gaetan_MORLET . The correct way to do it is as follows. Note the usage of the $[ ] and the $[<someId>] array update operators.Note on Array Update Operators:The latter two operators must be used with update operations on nested arrays. The $[<someId>] is used in conjunction with the arrayFilters update method option.
Code:$[ ]$[<someId>]db.collection.updateMany(
   { "ordDoc.custOrdItems.custOrdSubItems.prodId": "AC0074" },
   { $set: { "ordDoc.custOrdItems.$[].custOrdSubItems.$[e].prodId" : "AC0067" } },
   {
     arrayFilters: [ { "e.prodId": "AC0074"} ]
   }
)
$$[]$[<someId>]$[<someId>]arrayFilters

[Assistant]
Text:db.TestCollection.updateMany({“ordDoc.custOrdItems.custOrdSubItems.prodId”: “VU0074”},
{set: {“ordDoc.custOrdItems.custOrdSubItems…prodId”: “newValue”}})I will try the 2nd query.Also can i get the find query for both these options?You can just do that to find the documents to change.db.collection.find({“ordDoc.custOrdItems.custOrdSubItems.prodId”: “AC0074”})Can you also send a sample document from your collection @Mamatha_M ? We just have the return of the aggregation queries for the moment. It will allow us to see more clearly.
Code:

[Assistant]
Text:@Gaetan_MORLET If you see the following first query and output  in the original post by @Mamatha_M :Note the output has an array field custOrdSubItems. This is  after the aggregation’s $unwind stage: {$unwind:"$ordDoc.custOrderItems"}. This clearly tells that the ordDoc.custOrderItems is an array (the $unwind is applied only on arrays).So, the structure of the document has nested arrays. The outer array "ordDoc.custOrderItems" and the inner array "ordDoc.custOrderItems.custOrdSubItems" - and that is a nested array.
Code:db.test01.aggregate([{$unwind:"$ordDoc.custOrderItems"},
   {$match:{"ordDoc.custOrderItems.custOrdSubItems.prodId":"VU0074"}},
   {$project:{"ordDoc.custOrderItems.custOrdSubItems.prodId":1}}]).pretty()
{
   "ordDoc":{
      "custOrdItems":{
         "custOrdSubItems":[
            {
               "prodId":"VU0091"
            },
            {
               "prodId":"VU0074"
            },
            ...
         ]
      }
}
custOrdSubItems$unwind{$unwind:"$ordDoc.custOrderItems"}ordDoc.custOrderItems$unwind"ordDoc.custOrderItems""ordDoc.custOrderItems.custOrdSubItems"

[Assistant]
Text:Arf, I was editing my post
Here is what I wanted to write:I thought the custOrdItems field was an object … so we didn’t have nested arrays.
But the output of the first query + the unwind, we can effectively think that it’s rather an array.You are absolutely right. I based myself too much on the result of the query.
Code:

[User]
Text:i want to know where i can share as it is a big document.
Code:

[Assistant]
Text:You can share your document via pastebin.com if necessary @Mamatha_M.
Code:

[User]
Text:Hi Prasad,The find query which i had provided was returning almost 944 docs from that collection.
The update query which provided also updated 944 docs.
I guess it works but still i trying the same approach on the other nested arrays.
Now i would like to understand what it the function of this [] , [e] actually does?
and in arrayFilters u have provided e.prodId --> that point i didnt get it as I am new to mongodb. Can you please help me here?
Code:

[Assistant]
Text:Assume your input document is as following. There is an outer array custOrdItems within the ordDoc sub-document; the array’s path is "ordDoc.custOrdItems". Then, there is the array "ordDoc.custOrdItems.custOrdSubItems", and this is referred as nested array (an array within an array, the inner array).Now, you want to update the nested array "ordDoc.custOrdItems.custOrdSubItems"'s element, which is a sub-document { "prodId": "VU0074" }. The update has the condition that this sub-document’s prodId field value must be “VU0074”, and this is to be updated to a new value.The update operations on nested arrays with condition use the updateMany method’s option arrayFilters. The arrayFilters specifies the condition by which the nested array is to be updated - in this case the condition is that the prodId's value must be equal to “VU0074”. And, what about the condition for the outer array custOrdItems? There are no conditions there, and it means all the elements of this array are updateable.The update statement:The $set update operator updates a field’s value. In this case, we have to update the inner array’s field value based upon the condition (as discussed above). The update field’s path is specified as "ordDoc.custOrdItems.$[].custOrdSubItems.$[e].prodId".The two array update operators, the $[] and the $[e] are used here. The $[] operator is used when there is no condition on the  array element, in this case the outer array. The ordDoc.custOrdItems.$[] portion of the update field path says that.The remaining part of the path, custOrdSubItems.$[e].prodId says that the inner array element’s field to be updated. The e of the $[e] specifies the sub-document’s field for the condition, which is used with the arrayFilters. The e can be of any name (it is user defined, it could be subItem for example). The string “AC0067” is the new value to be updated with.Please go thru the documentation (the links I had provided earlier), and browse the details and the examples within.
Code:custOrdItemsordDoc"ordDoc.custOrdItems""ordDoc.custOrdItems.custOrdSubItems"{
   "ordDoc": {
      "custOrdItems": [
         "custOrdSubItems":[
            {
               "prodId": "VU0091"
            },
            {
               "prodId": "VU0074"
            },
            ...
         ]
      ]
    }
}
"ordDoc.custOrdItems.custOrdSubItems"{ "prodId": "VU0074" }prodIdupdateManyarrayFiltersarrayFiltersprodIdcustOrdItemsdb.collection.updateMany(
   { "ordDoc.custOrdItems.custOrdSubItems.prodId": "AC0074" },
   { $set: { "ordDoc.custOrdItems.$[].custOrdSubItems.$[e].prodId" : "AC0067" } },
   {
     arrayFilters: [ { "e.prodId": "AC0074"} ]
   }
)
$set"ordDoc.custOrdItems.$[].custOrdSubItems.$[e].prodId"$[]$[e]$[]ordDoc.custOrdItems.$[]custOrdSubItems.$[e].prodIde$[e]arrayFiltersesubItem

[User]
Text:Thanks for the detailed explanation.
Code:

[User]
Text:if i want to create an index on prodId – what index should go in and how should i create the index for arrays?
Code:

[Assistant]
Text:Array field values are indexed using Multikey Indexes.
Code:

[User]
Text:Hi Prasad,Nested arrays for the below aggregate query I am not able to update the key value to a newer value.I have put the script here and json format output. Can you pls help me how to update?JSON:
Code:db.test01.aggregate([{$unwind:"$ordDoc.custOrderItems"},
{$unwind:"$ordDoc.custOrderItems.custOrdSubItems"},
{$unwind:"$ordDoc.custOrderItems.custOrdSubItems.prodAtt"},
{$unwind:"$ordDoc.custOrderItems.custOrdSubItems.prodAtt.prices"},
{$unwind:"$ordDoc.custOrderItems.custOrdSubItems.prodAtt.prices.Key"},
{$match:{"ordDoc.custOrderItems.custOrdSubItems.prodAtt.prices.Key":"Technology=NONE"}},
{$project:{"ordDoc.custOrderItems.custOrdSubItems.prodAtt.prices.Key":1}}]).pretty()
/* 1 */
{
	"_id" : "ORN-1628755216489",
	"ordDoc" : {
		"custOrderItems" : {
			"custOrderSubItems" : {
				"prodAtt" : {
					"prices" : {
						"Key" : "Technology=NONE"
					}
				}
			}
		}
	}
},

/* 2 */
{
	"_id" : "ORN-3091717461503",
	"ordDoc" : {
		"custOrderItems" : {
			"custOrderSubItems" : {
				"prodAtt" : {
					"prices" : {
						"Key" : "Technology=NONE"
					}
				}
			}
		}
	}
},

[User]
Text:Team ,Any updates for the above ask?
Code: