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.
Internal Error! Value is nullauthorizedRequest.requestContentthat can't be serialized into BSON so that it can be stored in the collection. What does that contain?