duanli5662 2014-07-17 04:06
浏览 78
已采纳

MongoDB upsert - 子数组不会添加子数组

static::db()->resetError();
$wherehash = array("hashtag" => $hashtag[$j]);
$hash->update($wherehash,array("hashtag" => $hashtag[$j],"lastused"=>new MongoDate(), "stats" => array(array("statusId"=>new MongoId($status['_id']),"timestamp"=>new MongoDate()))),array('safe'=>true,'upsert' => true));

var_dump(static::db()->lastError());

Here is what it inserts

{
   "_id": ObjectId("53c7b94166e6eb75b52c599b"),
   "hashtag": "#hashtag",
   "lastused": ISODate("2014-07-17T12:03:58.980Z"),
   "stats": {
     "0": {
       "statusId": ObjectId("53c7bbaee7fda80e0d8b4567"),
       "timestamp": ISODate("2014-07-17T12:03:58.980Z") 
    } 
  } 
}

however when I try to post another status it should add it to stats and not override the subarray has it keeps doing.

I tried

$hash->update($wherehash,array("hashtag" => $hashtag[$j],"lastused"=>new MongoDate(), "stats" => array('$push'=>array("statusId"=>new MongoId($status['_id']),"timestamp"=>new MongoDate()))),array('safe'=>true,'upsert' => true)

and it did not work it added this to the collection

{
   "_id": ObjectId("53c7b94166e6eb75b52c599b"),
   "hashtag": "#hashtag",
   "lastused": ISODate("2014-07-17T12:31:20.738Z"),
   "stats": {
     "$push": {
       "statusId": ObjectId("53c7c218e7fda8641e8b4567"),
       "timestamp": ISODate("2014-07-17T12:31:20.738Z") 
    } 
  } 
}

展开全部

  • 写回答

1条回答 默认 最新

  • doubiao7410 2014-07-17 04:41
    关注

    I'm not a PHP developer but I guess the query should be constructed as follows:

    $hash->update($wherehash,
        array(
            "$set" => array(
                "hashtag" => $hashtag[$j],
                "lastused" => new MongoDate()
            ),
            "$push" => array(
                "stats" => array(
                    "statusId" => new MongoId($status['_id']),
                    "timestamp" => new MongoDate()
                )
            )
        ),
        array('safe' => true, 'upsert' => true)
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 游戏mod是如何制作的
  • ¥15 关于#hadoop#的问题:按照老师上课讲的步骤写的
  • ¥20 有人会用这个工具箱吗 付fei咨询
  • ¥30 成都市武侯区住宅小区兴趣点
  • ¥15 Windows软实时
  • ¥15 自有服务器搭建网络隧道并且负载均衡
  • ¥15 opencv打开dataloader显示为nonetype
  • ¥15 MacOS 80端口外网无法访问
  • ¥50 js逆转反解密-会的来
  • ¥15 wrodpress如何调取数据库并展示
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部