user1525721
user1525721

Reputation: 336

update an embedded document in mongodb using php

I am making a blogging site so I am using MongoDB as my DB. In my collection "articles", I have 1 embedded doc named posts and I want to insert 1 more embedded doc. comments into it.

DB:

  {
    "_id": ObjectId("4f41a5c7c32810e404000000"),
    "username":"abc",
    "posts": [
    {
        "_id": 1,
        "content": "dskcnkdcnksldcnskcd",
        "title" : "test1"
    },
    {"_id": 2,
        "content": "dskcsdsl;d,cl;sdcl;sdmcnkdcnksldcnskcd",
        "title" : "test2"
    },
   ]
}

I want to insert comments into posts.

 {
    "_id": ObjectId("4f41a5c7c32810e404000000"),
    "username":"abc",
    "posts": [
    {
        "_id": 1,
        "content": "dskcnkdcnksldcnskcd",
        "title" : "test1",
     "comments":[
    {
      "content": "usdhcjsdcjskd",
      "by" : "abc"
    }
    ]
    },
    {"_id": 2,
        "content": "dskcsdsl;d,cl;sdcl;sdmcnkdcnksldcnskcd",
        "title" : "test2"
    }
   ]
}

Upvotes: 0

Views: 3098

Answers (2)

Glibo
Glibo

Reputation: 196

the thing you need to be careful is your structure for first insert. For example you have some user with some lists and you want to let him to have up to 5 lists. This is how you can do it with PHP:

  1. You need to install your mongodb extension if you do not have it in your PHP environment on your localhost server

  2. You need to install mongodb and run it.

After all that is done you can easily do it like this:

$m = new Mongo("mongodb://localhost");
$db = $m->statistic->list;

// statistic is my database and list are collection.

$list =
        array(
            '_id' => 18,
            'lists' =>
            array(
                array(
                    'name' => 'Magento and PHP developer',
                    'recipients' =>
                    array(
                        'name' => 'Kristijan Glibo',
                        'email' => '[email protected]'
                    ),
                    'count' => 12345),
        ));

You can see here that I use some (int)18 for my _id. If you do not specify it, Mongo will generate it. I use it like this just for testing purposes. Notice here that lists are array and every list inside it are array too with their own data! My list Magento and PHP developer have data about recipients and count. Recipients also have some unique data.

Lets make first insert into our Mongodb:

$document = $db->insert($list);

If we now dump collection to check out what we saved inside of it we will get this:

NOTE: To get your collection and everything inside it you can use this code:

$documents = $db->find();
echo '<pre>';
foreach ($documents as $doc) {
    var_dump($doc);
}die();

********************this is dumped data*******************

["_id"]=>
  int(18)
  ["lists"]=>
  array(3) {
    [0]=>
    array(3) {
      ["name"]=>
      string(10) "Magento and PHP developer"
      ["recipients"]=>
      array(2) {
        ["name"]=>
        string(4) "Kristijan Glibo"
        ["email"]=>
        string(25) "[email protected]"
      }
      ["count"]=>
      int(12345)
    }

Lets now update our user and add him more lists:

$updatelist = array('name' => 'updatelist', 'recipients' => array(),'count'=>2876);
$documentupdate = $db->
update(
array('_id'=>18),
array('$addToSet' => array('lists'=> $updatelist)));

You are telling him which object to update by selectin _id => 18 and how to update. I test this and it will add new list if that list do not exist. If exist it will skip it.

Hope this helps!

Upvotes: 0

Sammaye
Sammaye

Reputation: 43884

Comments is actually an embedded document of posts in your document as such you need to change your query a little to:

$collection->update(array('_id' => new MongoId($bid),'posts.id'=> $pid),array('$push' => array('posts.$.comments' => $comment)));

Try that.

Notice how I have used the positional operator? You can read more about it here: http://www.mongodb.org/display/DOCS/Updating/#Updating-The%24positionaloperator

Upvotes: 3

Related Questions