1

In my node.js app, I need to store unstructured JavaScript objects in MongoDB. I have specified following model in Mongoose:

module.exports = mongoose.model('DBAllocation', {
    from: Date,
    expires: Date,
    userSession: Object,
    allocationTimestamp: Date,
    allocationPriority: Number,
    vmGroupID: String,
    allocationRequestContent: Object
});

By specifying the data types of userSession and allocationRequestContent to be type Object, I wanted to save a JavaScript object (without specifying its structure) into MongoDB and retrieve it as is. But when I save the model into database, I get an internal error. I tried to store following items:

var allocation = new Allocation({
                        _id: allocationID,
                        from: Date.now(),
                        expires: null,
                        userSession: authorizedRequest.session,
                        allocationTimestamp: Date.now(),
                        allocationPriority: <some number>,
                        vmGroupID: <some number>,
                        allocationRequestContent: authorizedRequest.requestContent
                    });

authorizedRequest.session and authorizedRequest.requestContent are two JavaScript Objects. But when I replace both of them with {}, model gets saved successfully. I have heard of strict parameter which we can use to store unstructured data, but I doubt whether I can use it to achive what I need. Is there a way to acomplish this anyway? Any help would be really appreciated.

Update:

I figured out that authorizedRequest.session is a MongoDB model and I replaced it with authorizedRequest.session.toObject() and replaced authorizedRequest.requestContent with a simple object such as {'cat': '123', 'dog':'456'} and it was saved successfully. Can't figure out what's going on.

authorizedRequest.requestContent includes following object.

{
        "group":[
            {
                "vm_count":[
                    "10"
                ],
                "image":[
                    {
                        "type":[
                            "iso"
                        ],
                        "id":[
                            "280b40d0-6644-4e47-ac7c-074e2fa40cd4"
                        ]
                    }
                ],
                "cpu":[
                    {
                        "cores":[
                            "1"
                        ],
                        "frequency":[
                            "1"
                        ],
                        "unit":[
                            "GHz"
                        ],
                        "architecture":[
                            "x86"
                        ]
                    }
                ],
                "min_memory":[
                    {
                        "size":[
                            "2"
                        ],
                        "unit":[
                            "GB"
                        ]
                    }
                ],
                "min_storage":[
                    {
                        "primary":[
                            "5"
                        ],
                        "unit":[
                            "GB"
                        ]
                    }
                ],
                "network":[
                    {
                        "min_bandwidth":[
                            "8"
                        ],
                        "unit":[
                            "mbps"
                        ]
                    }
                ],
                "priority":[
                    "3"
                ],
                "allocation_time":[
                    {
                        "schedule":[
                            {
                                "date":[
                                    {
                                        "$":{
                                            "year":"",
                                            "month":"",
                                            "date":""
                                        }
                                    }
                                ],
                                "time_from":[
                                    ""
                                ],
                                "time_to":[
                                    ""
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        "session_id":[
            "3deb1bb861f34b527e6709c655fff139b36c2dc43d8b3e29e3914bf8b23ce069"
        ]
    }

Thank you.

8
  • Tried the Schema.Types.Mixed ( mongoosejs.com/docs/schematypes.html ) ?
    – joao
    Feb 4, 2015 at 11:06
  • Yeah. But still does not work. It seems like only a simple object would be accepted. When the object itself contains nested objects, it gives an Internal Error.
    – Deepal
    Feb 4, 2015 at 12:27
  • Can you add the details of the error you're getting to your question?
    – JohnnyHK
    Feb 4, 2015 at 13:05
  • @JohnnyHK, I am not getting a detailed error! When debugging, I only get Internal Error! Value is null
    – Deepal
    Feb 4, 2015 at 13:54
  • @Deepal Based on your update, it seems that there's something in authorizedRequest.requestContent that can't be serialized into BSON so that it can be stored in the collection. What does that contain?
    – JohnnyHK
    Feb 4, 2015 at 13:58

1 Answer 1

1

The problem is likely the $ key in the authorizedRequest.requestContent object as MongoDB field names can't start with a $.

See the docs for possible work-arounds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.