Unable to unset properties on an embedded document

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 4.7.0
    • Affects Version/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Unable to unset properties on an embedded document in a model using the Laravel MongoDB package. The query generated by the package does not correctly unset the specified property.

       

      //Device class
      class Device extends Model {
      
          /**
           * The attributes that are mass assignable.
           *
           * @var array<string>
           */
          protected $fillable = [
              'type',
              'device_data'
          ];
      
          public function device_data()
          {
              return $this->embedsOne(DeviceData::class);
          }
      }
      // DeviceData class
      class DeviceData extends Model {
          /**
           * The attributes that are mass assignable.
           *
           * @var array<string>
           */
          protected $fillable = [
              'field1',
              'field2'
          ];
      } 
      // code
      $device = Device::find('66926851514af3dfe500d659');
      $data = $device->device_data;
      $data->unset('field1');
      $data->update();
      
      // However, the generated query is incorrect:
      db.devices.updateOne({"_id": ObjectId("66926851514af3dfe500d659")}, {"$set": {"device_data.$unset": {"field1": true}}})

      Expected behavior:

      db.devices.updateOne({"_id": ObjectId("66926851514af3dfe500d659")}, {"$unset": {"device_data.deviceType": true}}); 

       

       

              Assignee:
              Jérôme Tamarelle
              Reporter:
              Muhammad Memon
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: