Re: [mongoose] Model.update with dynamic field name

1,573 views
Skip to first unread message

Aaron Heckmann

unread,
Aug 22, 2012, 1:55:38 PM8/22/12
to mongoo...@googlegroups.com
This is just how javascript works. If you want to dynamically set your
properties do this:

var path ="comments.0";

var update = {};
update[path] = { text: 'test test' };

BlogModel.update({ _id: blog._id }, { $set: update }, function (err) {


On Wed, Aug 22, 2012 at 3:36 AM, dipesh acharya
<dipesh....@gmail.com> wrote:
> Hi,
>
> I am building a threaded comment with mongodb. My Schemas are as below.
>
>> var CommentSchema = new Schema({
>> name:String,
>> url:String
>> text:string
>> comments:[CommentSchema]
>> });
>
>
>> var BlogSchema = new Schema({
>> title:String,
>> body:String,
>> comments:[CommentSchema],
>> ....
>> });
>
>
> I was able to update Blog Model with hard coded path 'comments.0' as below.
>
>> BlogModel.update({ _id: blog._id }, { $set: { 'comments.0': { name:
>> 'dipesh',..,.. }}}, function (err) {
>> }
>
>
> However, trying to pass field name as variable, surprisingly it didn't work.
> Rather separate record with field name "path" gets added.
>>
>> var path ="comments.0";
>>
>> BlogModel.update({ _id: blog._id }, { $set: {path: { text: 'test test'
>> }}}, function (err) {
>> }
>
>
> Looks like I am missing some basic stuffs.
> How can I set the path variable for above update to work?
>
> Thank You,
> Dipesh
>
> --
> --
> http://mongoosejs.com
> http://github.com/learnboost/mongoose
> You received this message because you are subscribed to the Google
> Groups "Mongoose Node.JS ORM" group.
> To post to this group, send email to mongoo...@googlegroups.com
> To unsubscribe from this group, send email to
> mongoose-orm...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/mongoose-orm?hl=en
>
>
>



--
Aaron
@aaronheckmann

dipesh acharya

unread,
Aug 22, 2012, 7:25:04 PM8/22/12
to mongoo...@googlegroups.com
ta-da.. that worked like a charm! 
Big Thank You, Aaron. 

I also came to know, it had nothing to do with mongoosejs. 
It was about javascript bracket notation.

Thanks,
Dipesh
Reply all
Reply to author
Forward
0 new messages