0

I am trying to update() a specific single array in a collection, but while it works fine with $push parameter on a single, specific array, it does not work with a $set parameter.

I don't quite understand logic behind that, because when I use such an example of $pushing the element:

 $post_comment = array('$push' => 
    array("comments" => array(
    "_id" => new MongoId(),
    "comment" => htmlspecialchars($_POST['comment']),
    "author" => $user->username,
    "date" => new MongoDate()
      )
    )
  );
  $entries->update(array( 
    "_id" => $_GET["id"]), $post_comment);

It gives me an array in a MongoDB database which looks more or less like this (with four items pushed in, respectively) :

{
    "_id" : "css-clearfix-explained",
    "comments" : [ 
        {
            "_id" : ObjectId("540cc940af105b19133c9869"),
            "comment" : "aaa",
            "author" : "maciejsitko",
            "date" : ISODate("2014-09-07T21:08:16.215Z")
        }, 
        {
            "_id" : ObjectId("540cc943af105b19133c986a"),
            "comment" : "bbb",
            "author" : "maciejsitko",
            "date" : ISODate("2014-09-07T21:08:19.542Z")
        }, 
        {
            "_id" : ObjectId("540cc946af105b19133c986b"),
            "comment" : "ccc",
            "author" : "maciejsitko",
            "date" : ISODate("2014-09-07T21:08:22.968Z")
        }
    ]
}

Which is basically what I want to have, and logically, works fine according to the documentation. But when I try the same with $set as for to edit an individual comment, in the similar fashion as shown:

$edit_comment = array('$set' => 
    array("comments" => array(
    "_id" => new MongoId($_POST['cmt-id']),
    "comment" => htmlspecialchars($_POST['edit-comment']),
    "author" => $user->username,
    "date" => new MongoDate()
      )
    )
  );
  $entries->update(array( 
    "_id" => $_GET["id"]), $edit_comment);

It outputs four different arrays in place of the previous arrays, to illustrate that, i'll show what happened when I updated first comment "aaa" to "ddd" :

{
    "_id" : "css-clearfix-explained",
    "comments" : {
        "_id" : ObjectId("540cc940af105b19133c9869"),
        "comment" : "ddd\r\n                          ",
        "author" : "maciejsitko",
        "date" : ISODate("2014-09-07T21:12:10.833Z")
    }
}

All the four array elements were pretty much erased and in their place appeared four fields as four independent array elements.

How come? Shouldn't it just work just fine like the example with $push above?

1 Answer 1

2

You didn't specify an index within comments. Therefore, $set replaced the array comments with the associated array supplied. If you want to update a comment, then change your query in the first argument to match a comment by a unique field. Ex, date. In the second argument use a positional $ operator.

Example:

$edit_comment = array('$set' => 
  array("comments.$" => array(
  "_id" => new MongoId($_POST['cmt-id']),
  "comment" => htmlspecialchars($_POST['edit-comment']),
  "author" => $user->username,
  "date" => new MongoDate()
    )
  )
);
// this assumes the post date is unique. On second though use something else.
$query = array( "_id" => $_GET["id"], "comments.date" => $_POST['post-date'])
$entries->update( $query, $edit_comment);

Check this out for more info and better explanation: MongoDB - $set to update or push Array element

Sign up to request clarification or add additional context in comments.

2 Comments

Well, I applied this solution using the unique date, just to test it, when i click update nothing happens, and the same goes for the array, its unchanged. There is not even an error, but it does not quite work.
Okay, now it works, I just used something different than a MongoDate item, used $user->username from session login object, which works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.