使用 PHP 从 mongo 文档中删除数据 [英] Remove data from a mongo document with PHP

查看:50
本文介绍了使用 PHP 从 mongo 文档中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从 Mongo 获得的下一个数组:

I have the next array obtained from Mongo:

array(10) {
  ["_id"]=>
  object(MongoId)#25 (1) {
    ["$id"]=>
    string(24) "4ea062b9271e012227000000"
  }
  ["email"]=>
  string(18) "your@email.tld"
  ["group"]=>
  int(1)
  ["last_login"]=>
  int(1319299712)
  ["login_hash"]=>
  string(40) "a67b25998576d454c7a422908592ed338561a527"
  ["password"]=>
  string(44) "EK341WsJRo1vUB9vGYWfogfzstZsIg77/oRqlpeQH+I="
  ["profile_fields"]=>
  string(6) "a:0:{}"
  ["username"]=>
  string(7) "loremipsum"
  [0]=>
  string(10) "login_hash"
  [1]=>
  string(40) "9de77184cb57625f834879b3cbcdf0b860d842c1"
}

做测试时,我向该文档添加了错误信息,即数组的最后 2 个键:0 和 1.我的问题是如何使用 Mongo 和 PHP 删除该信息或其他标签?

Doing tests I have added bad information to that document, the last 2 keys of the array: 0 and 1. My question is how I can delete that info or another label with Mongo and using PHP?

先谢谢你!

推荐答案

在这种情况下,您需要的确切命令是:

In this case the exact command you need is:

db.collection.update(
    {"_id": ObjectID("4ea062b9271e012227000000")},
    {"$unset": [0: true, 1: true]}
);

在 php 中,这将类似于:

In php this would be like:

$mongoClient = new MongoClient();
$db = $mongoClient->selectDB('your_DB_name');
$collection = new MongoCollection($db, 'your_collection_name');
$collection->update(
    ['_id' => new MongoId("4ea062b9271e012227000000")],
    ['$unset' => [
        0 => true,
        1 => true
    ]]
);

这篇关于使用 PHP 从 mongo 文档中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆