1

以下是这样的内容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)));

但是记录没有删除

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

4

1 回答 1

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)));
于 2012-12-25T09:44:41.837 回答