Home > Software design >  Adding Node to exisitng documents mongodb
Adding Node to exisitng documents mongodb

Time:07-04

I am trying to update the existing document with one extra field as a new requirement in DB needs that

Existing field

[{
  "_id": {
    "$oid": "62a8ad644035e93c7ff24607"
  },
  "user_name": "[email protected]",
  "password": "1234",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "first_name": "ghgh",
    "middle_name": "",
    "last_name": "gh",
    "email": "[email protected]",
    "mobile_number": 1234567890
  },
  "address_details": {
    "country": {
      "country_id": {
        "$oid": "623012f9683f8fb7bd213c36"
      },
      "country_code": "IN"
    },
    "state": {
      "state_id": {
        "$oid": "623013d9683f8fb7bd2381c4"
      },
      "state_code": "MP"
    },
    "city": {
      "city_id": {
        "$oid": "6230139f683f8fb7bd21f1a9"
      },
      "city_name": "Akodia"
    },
    "address_line1": "",
    "address_line2": "",
    "pincode": ""
  },
  "is_verify": false,
  "last_accessed": {
    "$date": {
      "$numberLong": "1655221604144"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1655221604144"
    }
  },
  "course_details": {}
}] 

Needs to update all fields

[{
  "_id": {
    "$oid": "62bf2cf2a8c782741bbcc398"
  },
  "user_name": "[email protected]",
  "password": "$2b$12$NgLNYm5jUBmFUq.w15.qCO35GiTJX09jAnOpoGuPt9G7GNuOkVN7K",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "email": "[email protected]",
    "mobile_number": "9898989898",
    "first_name": "Fahim",
    "middle_name": "",
    "last_name": "Ashhab",
    "nationality": "Antiguans",
    "date_of_birth": "2003-02-04",
    "admission_year": "2022-23",
    "gender": "Male",
    "category": "General",
    "para_ability": {
      "is_disable": false,
      "name_of_disability": ""
    }
  },
  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  },
  "is_verify": true,
  "last_accessed": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "allocate_to_counselor": {
    "counselor_id": {
      "$oid": "62bfd13a5ce8a398ad101bd7"
    },
    "counselor_name": "test",
    "last_update": {
      "$date": {
        "$numberLong": "1656817948401"
      }
    }
  },
  "course_details": {
    "BSc": {
      "course_id": {
        "$oid": "628e03d94e22276b98231407"
      },
      "course_name": "BSc",
      "application_id": {
        "$oid": "62bf2cf2a8c782741bbcc399"
      },
      "status": "Incomplete",
      "specs": [
        {
          "spec_name": "Physician Assistant",
          "is_activated": true
        }
      ]
    }
  }
}]

Query Tried nothing is happening

db.collection.aggregate([
{$match:{"address_details.country": {$exists:true}}},
{$set:{address_details:{communication_address:"$address_details"}}}
])

2nd Query Tried didn't go through

db.studentsPrimaryDetails.updateMany({
    "address_details.communication_address": {"$exist":false}},
    {$set: {"address_details.communication_address":"$address_details"}})

The desired result needs to be from this

"address_details": {
    "country": {
      "country_id": {
        "$oid": "623012f9683f8fb7bd213c36"
      },
      "country_code": "IN"
    },
    "state": {
      "state_id": {
        "$oid": "623013d9683f8fb7bd2381c4"
      },
      "state_code": "MP"
    },
    "city": {
      "city_id": {
        "$oid": "6230139f683f8fb7bd21f1a9"
      },
      "city_name": "Akodia"
    },
    "address_line1": "",
    "address_line2": "",
    "pincode": ""
  }

to this

  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  }

Don't know what I am doing wrong.

Edit :

Desired full output

[{
  "_id": {
    "$oid": "62bf2cf2a8c782741bbcc398"
  },
  "user_name": "[email protected]",
  "password": "12345678",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "email": "[email protected]",
    "mobile_number": "9898989898",
    "first_name": "Fahim",
    "middle_name": "",
    "last_name": "Ashhab",
    "nationality": "Antiguans",
    "date_of_birth": "2003-02-04",
    "admission_year": "2022-23",
    "gender": "Male",
    "category": "General",
    "para_ability": {
      "is_disable": false,
      "name_of_disability": ""
    }
  },
  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  },
  "is_verify": true,
  "last_accessed": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "allocate_to_counselor": {
    "counselor_id": {
      "$oid": "62bfd13a5ce8a398ad101bd7"
    },
    "counselor_name": "viru chaudhary",
    "last_update": {
      "$date": {
        "$numberLong": "1656817948401"
      }
    }
  },
  "course_details": {
    "BSc": {
      "course_id": {
        "$oid": "628e03d94e22276b98231407"
      },
      "course_name": "BSc",
      "application_id": {
        "$oid": "62bf2cf2a8c782741bbcc399"
      },
      "status": "Incomplete",
      "specs": [
        {
          "spec_name": "Physician Assistant",
          "is_activated": true
        }
      ]
    }
  }
}]```

CodePudding user response:

One option is:

  1. Use $exists instead of $exist
  2. Use pipeline in order to be able to $set the value from another fields' value
db.collection.updateMany(
  {"address_details.communication_address": {$exists: false}},
  [{$set: {address_detailsB: {communication_address: "$address_details"}}},
   {$set: {
     address_details: "$address_detailsB",
     address_detailsB: "$$REMOVE"}
   }
])

See how it works on the playground example

The aggregation query that you first tried, does not not update the db, it only returns data. In order to use it as an updater you need to add a $merge step.

  • Related