A
A
Artqookie2011-08-22 18:50:16
PHP
Artqookie, 2011-08-22 18:50:16

Creating nested documents in PHP/MongoDB?

How to create a subdocument? There is a lot of information on the Internet on how to update (add) a nested document, but what if I don’t have a collection yet, but I need to immediately create its structure, with nested documents? I feel it is very simple :-)

$query = array(
  'author' => 'admin',
  'post' => array(
    'title' => '1',
    'body' => '1'
    ),
)

$collection->insert($query);

Creates a structure
{
  "_id" : ObjectId("4e5276adbd1ac4242b000002"),
  "author" : "admin",
  "post" :
  {
    "title" : "1",
    "body" : "1"
  }
}

And I seem to need one
{
  "_id" : ObjectId("4e5276adbd1ac4242b000002"),
  "author" : "admin",
  "post" :
  [
    {
      "title" : "1",
      "body" : "1"
    }
  ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VBart, 2011-08-22
@Artqookie

$query = array(
	'author' => 'admin',
	'post' => array(
    array(
		    'title' => '1',
		    'body' => '1'
		  )
  )
)

And deal with the terminology, the collection has no structure, it can contain any documents.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question