Is there a way to disable automatic creation of objects with empty arrays?

242 views
Skip to first unread message

Luka Blažecki

unread,
Apr 23, 2014, 4:34:42 AM4/23/14
to mongoo...@googlegroups.com
Hi

My schema looks like this : 

var object = new mongoose.Schema({
    target : target,
    ....
});

var target = {
     tags: {type: [String]},
     platforms : {type: [String]},
     geo : geo
}

var geo = {
    location_ids : {type: [String]},
    validity_time : {type: Number}
}

When creating object that looks like this : 
{
   target : {
       platforms : ["iOS"]
   }
}

Saved object looks : 
{
   target : {
       platforms : ["iOS"],
       tags : [],
       geo : {
          location_ids : []
       }
   }
}

I am expecting mongoose to save object as it is, so later when I use target field I know what fields are set and what aren't. With current way of saving object all arrays must be checked to see if they are empty to know what target fields to include.
Also there is no possibility to distinct fields with empty arrays and fields that aren't set.
So is there a way to disable automatic creation of objects with empty arrays?

Thank you!

Jason Crawford

unread,
Apr 26, 2014, 4:33:00 PM4/26/14
to mongoo...@googlegroups.com
I had this same problem, and I looked into it, and there was no easy way to fix this. I can't remember the reference I found for this, it might have been a Stack Overflow post. I tried to fix it for a while using a post-init hook, or something similar, to remove the empty arrays. But it was a losing battle. I can't remember exactly what problems I ran into, but eventually I gave up and just set a bit to track whether the array was applicable/meaningful or not.

-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.

Joe Wagner

unread,
Apr 27, 2014, 11:54:58 AM4/27/14
to mongoo...@googlegroups.com
Here is a SO post discussing an old issue around 2dshpere indexing.  Not exactly what you are asking about, but you could potentially use a similar solution.

Luka Blažecki

unread,
Jun 4, 2014, 9:37:23 AM6/4/14
to mongoo...@googlegroups.com
There is only one way.

Define that field as mongoose.Schema.Types.Mixed and validate that array by yourself.
Reply all
Reply to author
Forward
0 new messages