我有两个集合,其中一个是描述,即父项,带有图像,它是子项,并引用了描述集合。基于这种层次结构,我的集合就像这样设置。
描述:(示例文档)
{
"_id": "navy-blue",
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consequat nunc sit amet massa congue pharetra. Aliquam pretium arcu nec velit accumsan, nec pellentesque mauris porttitor. Donec ultrices interdum mi, in viverra nulla posuere sit amet. Donec quis elit nibh. Cras condimentum aliquam ullamcorper. In blandit, turpis a aliquam laoreet, sapien nunc venenatis nisl, vel blandit massa massa non lacus. Cras tempor enim non consequat iaculis. Curabitur quis urna tellus. Nunc at mattis justo. Donec dolor velit, placerat ut co"
}
images :(引用是description_id,示例文档)
{
"_id": {
"$oid": "55f0e2977f057f0d2f2ce548"
},
"pattern": "solid",
"propercasePattern": "Solid",
"color": "navy-blue",
"propercaseColor": "Navy Blue",
"imageUrl": "example.com/navy.jpg",
"imageSource": "http://test.com/",
"descriptions_id": "navy-blue"
}
作为此设置的结果,我正在尝试设置我的路线以查找所选的颜色和图案并显示匹配的所有图像文档,同时显示附加的说明。这种描述需要颜色而不是图案。
这是我的路线:(错误来自doc.descriptions_id控制台日志)
router.get('/:pattern/:color/result', function(req, res){
console.log(req.params.color);
Images.find( { pattern: req.params.pattern, color: req.params.color }).populate('descriptions_id').exec(function(err, image, doc){
if (err) { console.log(err); }
//console.log(image);
console.log(doc.descriptions_id);
res.render('pages/suit-result.hbs', {
pattern : req.params.pattern,
color : req.params.color,
image : image
});
});
});
以下是我当前设置的错误消息:
[CastError: Cast to ObjectId failed for value "navy-blue" at path "_id"]
stack: 'Error\n at MongooseError.CastError
message: 'Cast to ObjectId failed for value "navy-blue" at path "_id"',
name: 'CastError',
kind: 'ObjectId',
value: 'navy-blue',
path: '_id' }
为什么它在该值上失败,当它在mongoose文件中显示你应该使用id命名约定作为参考点?
更新
描述模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var descriptionSchema = new Schema({
color: String,
body: String,
});
var Description = mongoose.model('Description', descriptionSchema);
module.exports = Description;
图片模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var imageSchema = new Schema({
pattern: String,
color: { type: String, ref: 'Description'},
imageName: String,
imageUrl: String,
imageSource: String
});
var Images = mongoose.model('Images', imageSchema);
module.exports = Images;
答案 0 :(得分:0)
您的架构定义似乎不正确,或者至少您没有覆盖_id
的默认处理。您似乎也使用.populate()
调用了错误的路径,因为您的预期值应为" color"。
以下是一个示例:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var data = {
"_id": "navy-blue",
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consequat nunc sit amet massa congue pharetra. Aliquam pretium arcu nec velit accumsan, nec pellentesque mauris porttitor. Donec ultrices interdum mi, in viverra nulla posuere sit amet. Donec quis elit nibh. Cras condimentum aliquam ullamcorper. In blandit, turpis a aliquam laoreet, sapien nunc venenatis nisl, vel blandit massa massa non lacus. Cras tempor enim non consequat iaculis. Curabitur quis urna tellus. Nunc at mattis justo. Donec dolor velit, placerat ut co"
};
var descriptionSchema = new Schema({
_id: String,
body: String
},{ _id: false });
var imageSchema = new Schema({
color: { type: String, ref: 'Description' },
propercaseColor: String
});
var Description = mongoose.model('Description',descriptionSchema),
Image = mongoose.model('Image',imageSchema);
async.series(
[
function(callback) {
async.each([Description,Image],function(model,callback) {
model.remove({},callback);
},callback);
},
function(callback) {
async.waterfall(
[
function(callback) {
Description.create(data,callback);
},
function(desc,callback) {
Image.create({
"color": desc,
"propercaseColor": "Navy Blue"
},function(err) {
callback(err);
});
}
],
callback
);
},
function(callback) {
Image.find().populate('color').exec(function(err,doc) {
console.log(doc);
callback(err);
});
}
],
function(err) {
if (err) throw err;
mongoose.disconnect();
}
);
返回填充的对象:
[ { _id: 56060d1812966d29622f8a04,
color:
{ _id: 'navy-blue',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut consequat nunc sit amet massa congue pharetra. Aliquam pretium arcu nec velit accumsan, nec pellentesque mauris porttitor. Donec ultrices interdum mi, in viverra nulla posuere sit amet. Donec quis elit nibh. Cras condimentum aliquam ullamcorper. In blandit, turpis a aliquam laoreet, sapien nunc venenatis nisl, vel blandit massa massa non lacus. Cras tempor enim non consequat iaculis. Curabitur quis urna tellus. Nunc at mattis justo. Donec dolor velit, placerat ut co',
__v: 0 },
propercaseColor: 'Navy Blue',
__v: 0 } ]
当然没有.populate()
,它只是存储在" color"中的值。在图像集中:
[ { _id: 56060d5f81fff8356262d711,
color: 'navy-blue',
propercaseColor: 'Navy Blue',
__v: 0 } ]
因此,请确保您的架构已正确定义到" ref"模型,当然在链接模型中_id
都设置为正确的类型,当然配置禁用ObjectId
创建和通过{ _id: false }
进行投射。