1
votes

I've been struggling with the following Mongo document:

{
  "name": "Fire Name",
  "address": "123 Somestreet Ave",
  "city": "Boston",
  "state": "MA",
  "zip": "02109",
  "dispatchReference": "123codefromdispatch",
  "created": {
    "$date": "2021-02-26T12:30:41Z"
  },
  "lastPar": {
    "$date": "2021-02-26T22:30:41Z"
  },
  "latitude": "-83.691407",
  "longitude": "141.338391",
  "par": 3,
  "vehicles": [
    {
      "_id": "60397691c09c2fd299c40420",
      "hidden": false,
      "vehicleName": "Updated",
      "vehicleNumber": 20,
      "vehicleStatus": "Enroute",
      "latitude": "-83.691407",
      "longitude": "141.338391",
      "par": 6,
      "lastPar": "2021-02-26T22:30:41+00:00",
      "members": [
        {
          "_id": "60397b14f2a2b10978693a47", <---- find Member with this ID
          "hidden": false, <----- update this property
          "rank": "firefighter",
          "dateOfRank": "2021-02-26T22:49:56+00:00",
          "role": "hose",
          "firstName": "Reyna",
          "lastName": "Casey",
          "dateOfBirth": "1992-02-26T22:49:56+00:00",
          "sex": "male",
          "age": 29,
          "dateOfExperience": 5,
          "departmentLocation": "975 Pine Street, Gasquet, Montana, 1843",
          "lastIncidentID": "60397b14095d4f4313fbf716",
          "bpm": 120,
          "vomax": "33.1",
          "temp": "104.69",
          "latitude": "-81.393671",
          "longitude": "-78.26867",
          "scba": {
            "maskStatus": false,
            "oxygenLevel": "low"
          }

I'm attempting to write a Mongo query that when given the member._id, will update that specific member's hidden property from false to true. So I need to tunnel down into my vehicles array of objects, and find the member within the members array with the matching _id.

I suspect it has something to do with the $[] operator but I'm not having any luck.

1

1 Answers

0
votes

Try this:

db.testcollection.updateOne(
    { 
        // Specify some condition related to "vehicles" array.
        "vehicles.members._id": ObjectId("60397b14f2a2b10978693a47") 
    },
    {
        $set: {
            "vehicles.$.members.$[obj].hidden": true
        }
    },
    {
        arrayFilters: [
            {
                "obj._id": ObjectId("60397b14f2a2b10978693a47")
            }
        ]
    }
);