【问题标题】:Read a property of a array with Mongoose使用 Mongoose 读取数组的属性
【发布时间】:2013-01-09 05:24:23
【问题描述】:

这是架构:

var userschema = new mongoose.Schema({

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

              title: String,
              author: String,
              description: String,
              index: Number,
              path: String,

           }]

});

我正在尝试使用 'foomodel.find',在 Schema 的 imagen 数组中查找具有特定属性的元素,我的意思是:

usermodel.find({ user: foo, 'imagen.index': 1 }, function (err, imagen){

        if(err) throw err;

        console.log(imagen);

});

在控制台中,我收到了这个[],当我想收到imagen 数组元素的属性时,它的索引为1。我检查了它是否存在。有什么解决办法...?

感谢您的提前!

【问题讨论】:

    标签: javascript node.js mongoose


    【解决方案1】:

    我认为你需要使用$elementMatch:

    usermodel.find({ user: foo, "imagen": {"$elemMatch":{"index": 1 }}, 
        function (err, imagen){
            if(err) throw err;
            console.log(imagen);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2018-07-05
      • 2014-06-22
      • 2022-01-27
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多