0

This is my expected result:

 "ForgotPassword": [
     {
       "UpdatedOn": ISODate("2017-12-06T11:23:23.0Z"),
    },
     {
       "UpdatedOn": ISODate("2017-12-06T11:45:13.0Z"),
    }
  ]

And this what I am getting:

"ForgotPassword": {
     "UpdatedOn": [
       ISODate("2017-12-20T11:48:15.0Z"),
       ISODate("2017-12-20T11:48:30.0Z"),
       ISODate("2017-12-21T11:57:21.0Z") 
    ] 
  } 

Actually Forgot password field will not present in the collection document.

When I add the first time it should create Forgotpassword field and inside updatedon should store

And

when I add the second time the inside the Forgotpassword, updatedon should repeat for me it is storing inside updateon

This is my query:

 $updateQuery  =   $queryUpdate->update(

 array("CollaboratorId"=>(int)$collaboratorId), //query condition

 array('$addToSet'=>array('ForgotPassword.UpdatedOn'=>$currentDate)),

 array('upsert'=>1)

 );
Mr world wide
  • 4,696
  • 7
  • 43
  • 97
  • 1
    Try `array('$addToSet'=>array('ForgotPassword'=>array('UpdatedOn'=>$currentDate)))` – s7vr Dec 21 '17 at 13:56

1 Answers1

0
$queryUpdate->findAndModify( 
array("CollaboratorId"=> (int)$collaboratorId),
array('$addToSet'=> array('ForgotPassword' =>array("UpdatedOn" => $currentDate))),
array('new' => 1,"upsert"=>1)
);

With upsert and new and findandmodify and addtoset it is working

Mr world wide
  • 4,696
  • 7
  • 43
  • 97