Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$unset not properly work on multiple field? #1279

Open
vaporizel opened this issue Apr 17, 2024 · 1 comment
Open

$unset not properly work on multiple field? #1279

vaporizel opened this issue Apr 17, 2024 · 1 comment

Comments

@vaporizel
Copy link

I want to remove all these field below from certain record :

  • membership_active_time (integer)
  • membership_renewal_time (date)
  • membership_start_date (date)
  • membership_type (text)
  • metadata (object of field)
  • vendor_payment (text)

but the problem is only remove 2 field : membership_renewal_time and metadata, this is my code :

$this->result = $this->user_collection->updateOne(
	['owner_user_code' => $this->input_owner_user_code],
	['$unset' => 
		[
			'membership_active_time' => true,
			'membership_renewal_time' => true,
			'membership_start_date' => true,
			'membership_type' => true,
			'metadata' => true,
			'vendor_payment' => true,
		],
	]
);

im also tried with => false, => "", => '', but still only deletes certain field like i said above

@jmikola
Copy link
Member

jmikola commented Apr 17, 2024

The syntax you have looks correct. Note that the value of the fields in the $unset document are not relevant. Only the keys are used to discern the field names.

<?php

require __DIR__ . '/../vendor/autoload.php';

use MongoDB\BSON\UTCDateTime;
use MongoDB\Client;

$client = new Client('mongodb://localhost:27070/?replicaSet=rs0');

$coll = $client->selectCollection('test', 'coll');
$coll->drop();
$coll->insertOne([
    'owner_user_code' => 1,
    'membership_active_time' => 100,
    'membership_renewal_time' => new UTCDateTime(),
    'membership_start_date' => new UTCDateTime(),
    'membership_type' => 'foo',
    'metadata' => ['x' => 1],
    'vendor_payment' => 'bar',
]);

var_dump($coll->findOne());

$updateResult = $coll->updateOne(
    ['owner_user_code' => 1],
    ['$unset' => [
        'membership_active_time' => true,
        'membership_renewal_time' => true,
        'membership_start_date' => true,
        'membership_type' => true,
        'metadata' => true,
        'vendor_payment' => true,
    ]],
);

printf(
    "\nupdateOne matched %d and modified %d\n\n",
    $updateResult->getMatchedCount(),
    $updateResult->getModifiedCount(),
);

var_dump($coll->findOne());

Output:

object(MongoDB\Model\BSONDocument)#18 (1) {
  ["storage":"ArrayObject":private]=>
  array(8) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#23 (1) {
      ["oid"]=>
      string(24) "661fcfc37a3fa8243500cdb2"
    }
    ["owner_user_code"]=>
    int(1)
    ["membership_active_time"]=>
    int(100)
    ["membership_renewal_time"]=>
    object(MongoDB\BSON\UTCDateTime)#22 (1) {
      ["milliseconds"]=>
      string(13) "1713360835821"
    }
    ["membership_start_date"]=>
    object(MongoDB\BSON\UTCDateTime)#17 (1) {
      ["milliseconds"]=>
      string(13) "1713360835821"
    }
    ["membership_type"]=>
    string(3) "foo"
    ["metadata"]=>
    object(MongoDB\Model\BSONDocument)#15 (1) {
      ["storage":"ArrayObject":private]=>
      array(1) {
        ["x"]=>
        int(1)
      }
    }
    ["vendor_payment"]=>
    string(3) "bar"
  }
}

updateOne matched 1 and modified 1

object(MongoDB\Model\BSONDocument)#13 (1) {
  ["storage":"ArrayObject":private]=>
  array(2) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#19 (1) {
      ["oid"]=>
      string(24) "661fcfc37a3fa8243500cdb2"
    }
    ["owner_user_code"]=>
    int(1)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants