dqjjw04440 2012-12-25 09:15 采纳率: 0%
浏览 150
已采纳

如何删除mongodb中的json元素?

Following is the content in mongodb like this:

{
"_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
    }
 }
}

I want to delete :

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

The result will be:

{
"_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
    }
 }
}

I wrote the code like this:

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

But the record didn't remove

How can i write the code?
Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douchao9899 2012-12-25 09:44
    关注

    You can remove it with this:

    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)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?