Proper use of document Document#set with arrays

44 views
Skip to first unread message

Mark Sanford

unread,
Jun 4, 2014, 4:16:08 PM6/4/14
to mongoo...@googlegroups.com
How do I set a value within a document, if it's nested within an array?   Setting properties using Document#set(path, value)  within nested objects works fine.   If there is a array in the path, not so much.  e.g.:


 var schema = new mongoose.Schema({
    name
: {
      first
: String,
     
last  : String,
   
},
    points
: [ { x: Number, y: Number } ],
 
});
 
var Shape = mongoose.model( 'Shape', schema );
 
 
var s = new Shape({
    name
: {
      first
: 'frank',
     
last  : 'smith',
   
},
    points
: [
     
{ x : 1, y : 1 },
     
{ x : 2, y : 2 },
   
],
 
});


  console
.log( JSON.stringify( s, undefined, 2 ) );
 
/*
  as expected:
    {
      "_id": "538f7cfb5fa6f8702350a78a",
      "points": [
        {
          "x": 1,
          "y": 1,
          "_id": "538f7cfb5fa6f8702350a78c"
        },
        {
          "x": 2,
          "y": 2,
          "_id": "538f7cfb5fa6f8702350a78b"
        }
      ],
      "name": {
        "first": "frank",
        "last": "smith"
      }
    }


  */

 
  s
.set( 'points.1.y', 10 );
  s
.set( 'name.first', 'James' );


  console
.log( JSON.stringify( s, undefined, 2 ) );
 
/*
    s.set completely clobbered s.points[1].  x and _id props are gone!


    {
      "_id": "538f7cfb5fa6f8702350a78a",
      "points": [
        {
          "x": 1,
          "y": 1,
          "_id": "538f7cfb5fa6f8702350a78c"
        },
        {
          "y": 10
        }
      ],
      "name": {
        "first": "James",
        "last": "smith"
      }
    }    
  */

Jason Crawford

unread,
Jun 4, 2014, 4:28:14 PM6/4/14
to mongoo...@googlegroups.com
I don't know why your set call isn't working as expected, but why not just do:

s.points[1].y = 10;
s.name.first = 'James';

That's why the schema exists, so you can write straightforward expressions like that.

Hope that helps,
Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford




--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Sanford

unread,
Jun 4, 2014, 4:45:25 PM6/4/14
to mongoo...@googlegroups.com
Thanks for your reply.   

My use case involves storing a path within a document as a string and accessing the value through that path.   I created the example to demonstrate in the simplest way why document#set does not work as I was expecting.  


William Riley-Land

unread,
Jun 6, 2014, 5:54:16 PM6/6/14
to mongoo...@googlegroups.com
Sounds like it could be a bug.  You might consider adding an issue on the GitHub page.  

William Riley-Land
Founder & API Engineer
Kun.io App Studio LLC
http://kun.io/wprl
+1.573.823.2607
 
On Jun 4, 2014, at 3:45 PM, Mark Sanford <markts...@gmail.com> wrote:

Thanks for your reply.   

My use case involves storing a path within a document as a string and accessing the value through that path.   I created the example to demonstrate in the simplest way why document#set does not work as I was expecting.  



Reply all
Reply to author
Forward
0 new messages