Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Basically I got my app up an running but I'm stuck with a problem: if I pass an object that contains an empty array to be saved, the array is not saved into the db. I'm not sure this is a problem in js or the mongo driver, but in order to save the empty array I need to pass the array like so: products: [''].

This is the structure of my mongo document:

_id: ObjectId(...),
name: 'String',
subcategories: [
    {
        subcategory: 'string',
        products: [
            {
                 name: 'string'
                 price: interger
            }
        ]
    }
]

So in my front-end I'm grabbing the whole document through an ajax call pushing a new object into the subcategories array. The new object looks like this:

{subcategory:'string', products:['']}

And this works okay until I need to insert a new object inside the array: Because I've grabbed the whole object, pushed the new object to the array, the previous one looks like this:

{subcategory: 'string'}

Having lost the mention to products:[] array in the process.

How can I get around this? I need to be able to have empty arrays in my object.

EDIT

What I did on front end: Got the whole object with $.get which returned:

var obj = 
_id: ObjectId(...),
name: 'String',
subcategories: [
    {
        subcategory: 'Subcategory1',
        products: [
            {
                 name: 'string'
                 price: interger
            }
        ]
    }
];

Then on the front end I've pushed the new object category inside the subcategories array:

data.subcategories.push({subcategory: 'Subcategory2', products: ['']})

Where subcat was a string with the category name. On my db I could see that I've successfully added the object:

var obj = 
_id: ObjectId(...),
name: 'String',
subcategories: [
    {
        subcategory: 'Subcategory1',
        products: [
            {
                 name: 'string'
                 price: interger
            }
        ]
    },
    {
         subcategory: 'Subcategory2'
         products: []
    }
];

The problem was when I wanted to add another subcategory, the previous one return empty:

var obj = 
_id: ObjectId(...),
name: 'String',
subcategories: [
    {
        subcategory: 'Subcategory1',
        products: [
            {
                 name: 'string'
                 price: interger
            }
        ]
    },
    {
         subcategory: 'Subcategory2'
    },
    {
         subcategory: 'Subcategory3'
         products: []
    },
];

Because at some point the empty array was removed from the object. Like I said, I did fix this in the front end, so the error jade was throwing has been addressed, but I still find odd that the products: [] was being removed from the document.

I'm new to MongoDb and node, not to mention that I'm also new with JS, so it might well be a feature that I'm unaware of.

share|improve this question
    
Could you explain why you need to do this? MongoDB intentionally doesn't save "empty" data (which an empty array would be). I'd suggest that the client code in NodeJS should provide a default value. –  WiredPrairie Nov 17 '13 at 15:10
    
empty array is NOT empty data. there is a big difference between products: null or products: not existing and products: [ ] - for one it's a different BSON type. It's possible there is a bug - OP, it's not 100% clear to me what your code is doing. When you say you "grabbed the whole object" which object are you referring to? –  Asya Kamsky Nov 17 '13 at 19:56
    
Can you give exact code you use to update the document? Sounds like you may not be using update() with $push but pushing locally in the client code? also, what version of the driver are you using and what version of MongoDB? –  Asya Kamsky Nov 17 '13 at 19:57
    
Ok, first things first: I was getting an error on the front end as Jade couldn't loop through an undefined object (if the array was not saved category.subcategories was retuning as undefined). I fixed with an conditional in the front end so the error is gone. @AsyaKamsky What I did in the from end was grab the whole object as described above then pushed the changes into the array. –  WagnerMatosUK Nov 17 '13 at 20:20
    
You are saying that when you save({_id:1, foo:[{bar:'x',arr:[]}]}) wha t ends up saved in the db is {_id:1, foo:[{bar:'x'}]} is that right? and that's reproducible? –  Asya Kamsky Nov 17 '13 at 20:26
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.