1

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 Answer 1

0

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")
            }
        ]
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god that worked! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.