GithubHelp home page GithubHelp logo

Comments (1)

jmikola avatar jmikola commented on July 3, 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)
  }
}

from mongo-php-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.