0
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") 
    } 
  } 
}
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

1 Answers1

0

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)
);
Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82