如何删除mongodb中的json元素?

时间:2012-12-25 09:15:13

标签: php mongodb mongodb-php

以下是mongodb中的内容,如下所示:

{
"_id": 74924,
"bangs": {
    "436": {
        "join_time": 1345624851,
        "status": 1,
        "scores": 0
    },
    "446": {
        "join_time": 1355727257,
        "status": 1,
        "scores": 0
    }
},
"_id": 74926,
"bangs": {
    "436": {
        "join_time": 1345624851,
        "status": 1,
        "scores": 0
    },
    "446": {
        "join_time": 1355727257,
        "status": 1,
        "scores": 0
    }
 }
}

我想删除:

"436": {
        "join_time": 1345624851,
        "status": 1,
        "scores": 0
    },
 where _id=74924

结果将是:

{
"_id": 74924,
"bangs": {
    "446": {
        "join_time": 1355727257,
        "status": 1,
        "scores": 0
    }
},
"_id": 74926,
"bangs": {
    "436": {
        "join_time": 1345624851,
        "status": 1,
        "scores": 0
    },
    "446": {
        "join_time": 1355727257,
        "status": 1,
        "scores": 0
    }
 }
}

我写了这样的代码:

$mongo->users->users->update(array('_id'=>74924), array('$pull'=>array('bangs'=>436)));

但记录没有删除

我该如何编写代码?
提前谢谢!

1 个答案:

答案 0 :(得分:1)

你可以用它删除它:

db.test.update( { _id:74924 }, { $unset: {"bangs.436":1} } )

// I think this can be written in php like this:
$mongo->users->users->update(array('_id'=>74924), array('$unset'=>array('bangs.436'=>1)));