显示Mongoose模式中的特定数组

时间:2013-01-14 15:33:03

标签: node.js mongodb mongoose

这是我正在使用的架构:

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画质:      [        {title:'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 个答案:

答案 0 :(得分: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/