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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失