how to add values to array in an existing article

27 views
Skip to first unread message

sdotsen

unread,
Aug 17, 2009, 11:39:44 AM8/17/09
to mongodb-user
Going back to a blogging system. Let's say I have an article and it
has an array called "blog_comments" with two fields, author and
comments.

{ "_id" : "4a8977b0eb285a483ffe7d5b", "blog_title" : "hello",
"blog_content" : "hello world", "blog_url" : "hello", "date" :
"0.66139400 1250523056", "blog_comments" : { "author" : "billbob",
"comments" : "very first comment" } }

I tried the following which fails miserably.

db.articles.update( {blog_title: "hello"}, { "blog_comments" :
{ $push : {"author": "jimbob", "comments" : "Trying to add second
comment" } } } )

Eliot Horowitz

unread,
Aug 17, 2009, 11:44:05 AM8/17/09
to mongod...@googlegroups.com
The problem is that blog_comments is an object initially and not an array.

the the inital object should be:
{ "_id" : "4a8977b0eb285a483ffe7d5b",
"blog_title" : "hello",
"blog_content" : "hello world",
"blog_url" : "hello",
"date" : "0.66139400 1250523056",
"blog_comments" : [ { "author" : "billbob", "comments" : "very first
comment" } ]
}

also: how are you inserting the date? that looks like a double, not a Date type

sdotsen

unread,
Aug 17, 2009, 11:55:12 AM8/17/09
to mongodb-user
wait ... you're right crap ... what did i do wrong? here's my PHP
code.

$doc = array( "blog_title" => "$article[title]",
"blog_content" => "$article[content]",
"blog_url" => "$clean_url",
"date" => "$d",
"blog_comments" => array( "author" => "",
"comments" => "",
)
);

And I got the date from

$d = new MongoDate();



On Aug 17, 11:44 am, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> The problem is that blog_comments is an object initially and not an array.
>
> the the inital object should be:
> { "_id" : "4a8977b0eb285a483ffe7d5b",
>     "blog_title" : "hello",
>     "blog_content" : "hello world",
>   "blog_url" : "hello",
>   "date" : "0.66139400 1250523056",
> "blog_comments" : [ { "author" : "billbob", "comments" : "very first
> comment" } ]
>
> }
>
> also: how are you inserting the date?  that looks like a double, not a Date type
>

Kristina Chodorow

unread,
Aug 17, 2009, 12:00:32 PM8/17/09
to mongod...@googlegroups.com
Right now you're creating:

"blog_comments" : { "author" : ..., "comments" : ...}

Whereas you want:

"blog_comments" : [{ "author" : ..., "comments" : ...}]

So you just need to nest it in another array:

"blog_comments" => array( array( "author" => "",
                                             "comments" => ""))

As far as the date goes, remove the quotes.  Saying:

"date" => "$d"

is converting the MongoDate to a string, then saving it.  You probably want:

"date" => $d

sdotsen

unread,
Aug 17, 2009, 12:11:11 PM8/17/09
to mongodb-user
PERFECT the date and updating works ... i grabbed the way I was
handling the array from here under "inserting a document."

http://www.mongodb.org/display/DOCS/PHP+Tutorial




On Aug 17, 12:00 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> Right now you're creating:
>
> "blog_comments" : { "author" : ..., "comments" : ...}
>
> Whereas you want:
>
> "blog_comments" : [{ "author" : ..., "comments" : ...}]
>
> So you just need to nest it in another array:
>
> "blog_comments" => array( array( "author" => "",
>                                              "comments" => ""))
>
> As far as the date goes, remove the quotes.  Saying:
>
> "date" => "$d"
>
> is converting the MongoDate to a string, then saving it.  You probably want:
>
> "date" => $d
>

sdotsen

unread,
Aug 17, 2009, 12:20:43 PM8/17/09
to mongodb-user
For the date, without the "quote" the record shows as this.
is that correct?

{ "_id" : "4a8977b0eb285a483ffe7d5b",
"blog_title" : "hello",
"blog_content" : "hello world",
"blog_url" : "hello",
"date" : { "$date" : 1250525288117 },
"blog_comments" : [ { "author" : "billbob", "comments" : "very first
comment" } ]

}



On Aug 17, 12:00 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> Right now you're creating:
>
> "blog_comments" : { "author" : ..., "comments" : ...}
>
> Whereas you want:
>
> "blog_comments" : [{ "author" : ..., "comments" : ...}]
>
> So you just need to nest it in another array:
>
> "blog_comments" => array( array( "author" => "",
>                                              "comments" => ""))
>
> As far as the date goes, remove the quotes.  Saying:
>
> "date" => "$d"
>
> is converting the MongoDate to a string, then saving it.  You probably want:
>
> "date" => $d
>

Kristina Chodorow

unread,
Aug 17, 2009, 12:26:12 PM8/17/09
to mongod...@googlegroups.com
The date field shouldn't be in {}s (or have a '$date' key).  What is the code you're using to insert this document?  It looks like you're adding an extra array for the date field, like:

$d = array('$date' => new MongoDate());

It should be something like:

$d = new MongoDate();

So the whole object is:

array("blog_title" => "hello",
"blog_content" => "hello world",
"blog_url" => "hello",
"date" => new MongoDate(),
"blog_comments" => array(array("author"...)))

sdotsen

unread,
Aug 17, 2009, 12:41:40 PM8/17/09
to mongodb-user
$d = new MongoDate();

$doc = array( "blog_title" => "$article[title]",
"blog_content" => "$article[content]",
"blog_url" => "$clean_url",
"date" => $d,
"blog_comments" => array(array( "author" =>
"jimbob",
"comments" => "bob",
))
);




On Aug 17, 12:26 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> The date field shouldn't be in {}s (or have a '$date' key).  What is the
> code you're using to insert this document?  It looks like you're adding an
> extra array for the date field, like:
>
> $d = array('$date' => new MongoDate());
>
> It should be something like:
>
> $d = new MongoDate();
>
> So the whole object is:
>
> array("blog_title" => "hello",
> "blog_content" => "hello world",
> "blog_url" => "hello",
> "date" => new MongoDate(),
> "blog_comments" => array(array("author"...)))
>

Jon Hancock

unread,
Aug 17, 2009, 1:34:53 PM8/17/09
to mongodb-user
Here's mongo-ruby-driver test code showing how to use push, pushAll,
pull, and pullAll:
http://gist.github.com/169258

Jon

sdotsen

unread,
Aug 17, 2009, 6:38:05 PM8/17/09
to mongodb-user
So, anyone have any ideas as to why "date" => $d spits out the wrong
format?
Reply all
Reply to author
Forward
0 new messages