Comments (1)
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)
- PHP+MongoDB: "Return value must be of type MongoDB\Driver\Server, null returned" HOT 3
- Can we drop `jean85/pretty-package-versions:1.2`? HOT 4
- Decision about `mongo-orchestration` folder and `Makefile` HOT 3
- [Documentation] Syntax highlighting in the method reference is not ideal HOT 1
- A never-ending query after upgrade to MongoDB 6.0.3 HOT 6
- Memory leak on updateOne in the loop HOT 1
- Deprecated: Return type of MongoDB\Model\BSONDocument::bsonSerialize() HOT 2
- The StreamWrapper class is currently unusable without the bucket HOT 4
- Performance issue with PHP 8.2 HOT 2
- Extract mongodb-1.6.2.tgz error HOT 1
- MacOs M1 Cannot connect to Atlas After upgrade driver HOT 7
- This is a test
- Docs: missing `]` character in database->aggregate documentation HOT 1
- Question about fieldPaths (feature request?) HOT 1
- Non blocking io HOT 1
- $where is not allowed in this context HOT 2
- TLS connection with mongo cluster failed (while single host works) HOT 2
- getting result from mongodb with toArray() HOT 3
- Getting the error in driver version 1.14 HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongo-php-library.