2013-01-14 126 views
0

这是我使用的模式显示一个特定的阵列:从猫鼬模式

var userschema = new mongoose.Schema({ 

    user: String, 
    pass: String, 
    imagen: [{ 

       title: String, 
       name: String, 
       description: String 

}); 

这是我在做什么:

usermodel.find({ 'imagen._id': req.params.id }, function (err, imagen){ 

    user.imagen 

    if(err) throw err; 
    console.log(imagen); 

    res.send(imagen); 

}); 

我想收到什么只是imagen阵列中的元素我正在寻找_id,但取而代之的是,我收到了用户的洞图,他的所有图像都是白色的。有什么只接收我正在寻找的数组对象?这是一个例子:

[{__v:3, _id:50f41ccff405ef73c4000006, 通: 'mangakas123', 用户: 'kirbo', imagen画质: [ {标题: 'DQ monstes', 名: 'DragonQuest1and2EnemySpriteGallery_02.png', 描述: 'DQ怪兽的汇编', _id:50f41f868e7f9919c7000006}], 时间表:[], 通知:[], 追随者:[ '50f41c8c59ebd50fc4000006'], 如下:[ ]}]

我想要这个:

[{ title: 'DQ monstes', 
    name: 'DragonQuest1and2EnemySpriteGallery_02.png', 
    description: 'A compilation of DQ monsters', 
    _id: 50f41f868e7f9919c7000006 }] 

谢谢先遣!

回答

1

可以排除其他领域类似这样的

usermodel.find({ 'imagen._id': req.params.id },{'imagen':true}, function (err, imagen){ 

    user.imagen 

    if(err) throw err; 
    console.log(imagen); 

    res.send(imagen); 

}); 

见find()方法的第二个参数:http://docs.mongodb.org/manual/reference/method/db.collection.find/

+0

它的工作原理,但不知道如何我的预期。我得到了hole'image'数组,不仅是我正在寻找的'_id'的数组元素,并且控制台在显示'imagen'数组后显示出这个错误:'Can not read property'_id'null' 。为什么会发生?猫鼬完美地读取数组,并在控制台中显示它。 – MrMangado