在我的node.js应用程序中,我需要在MongoDB中存储非结构化JavaScript对象。我在Mongoose中指定了以下模型
module.exports = mongoose.model('DBAllocation', {
from: Date,
expires: Date,
userSession: Object,
allocationTimestamp: Date,
allocationPriority: Number,
vmGroupID: String,
allocationRequestContent: Object
});通过将userSession和allocationRequestContent的数据类型指定为Object类型,我希望将JavaScript对象(没有指定其结构)保存到MongoDB中,并按原样检索它。但是当我将模型保存到数据库中时,我会得到一个内部错误。我试着储存以下物品:
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和authorizedRequest.requestContent是两个JavaScript对象。但是当我用{}替换它们时,模型就会被成功地保存起来。我听说过strict参数,我们可以使用它来存储非结构化数据,但我怀疑我是否能够使用它来实现我所需要的。有办法解决这件事吗?任何帮助都会很感激的。
更新:
我发现authorizedRequest.session是一个MongoDB模型,我用authorizedRequest.session.toObject()替换了它,用一个简单的对象代替了authorizedRequest.requestContent,比如{'cat':'123','dog':'456'},并且成功地保存了它。不知道怎么回事。
authorizedRequest.requestContent包括以下对象。
{
"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"
]
}谢谢。
发布于 2015-02-05 02:28:07
问题很可能是$对象中的authorizedRequest.requestContent键,因为MongoDB字段名不能以$开头。
有关可能的工作,请参见医生们。
https://stackoverflow.com/questions/28317930
复制相似问题