unsetting fields when pushing an object to array

8 views
Skip to first unread message

imri heppner

unread,
Aug 28, 2013, 2:48:10 AM8/28/13
to mongoo...@googlegroups.com
Hi,

my problem is with mongoose setting  [ ]  for arrays without asking it to do so (in update - while pushing objects)

i am trying to do something like this:
var obj = {"field1": "some text"}
MyModel.findOneAndUpdate({"@ID": id}, {$push: {"objArray":obj}}


the simplified schema is:
var secSchema = new Schema({"field1": String, "field2":[String]});
var MyModelSchema = new Schema({"objArray":[secSchema]});

the problem is the the result object in the DB looks like:
{ "_id": ObjectId("asdasdgsdfgasf134sdf"),
 
"objArray" : [ {"field1": "sometext",
               
"field2": []}
           
]
}

my problem is with the 'field2'  - i don't want it to be set to [ ].

the reason is that i have many fields like that - and just once in a while they are being set - so i don't want to save it empty, it also make it difficult to use the {$exist} option later in queries.
(i saw this post but didn't manage to work it out since i am pushing pure json object and not a model.save)

is there any solution?
thanks!
Reply all
Reply to author
Forward
0 new messages