0

我执行以下操作:

var peoples = db.model('peeps', new mongoose.Schema());
peoples.find({}, {weights: 1}).exec(function(err, data) {
  // data is an array with objects
  data.forEach(function(el, index, array) {
    console.log(el); // --> {weights: [{weight: 45.78, diet: ln}, {weight: 21.89, diet: lgt}]}
    consoel.log(el.weights) // --> undefined
} ... });

但是当我建议这样的模式时:

var peoples = db.model('peeps', new mongoose.Schema({
   weights: []
   }));
peoples.find({}, {weights: 1}).exec(function(err, data) {
  // data is an array with objects
  data.forEach(function(el, index, array) {
    console.log(el); // --> {weights: [{weight: 45.78, diet: ln}, {weight: 21.89, diet: lgt}]}
    consoel.log(el.weights) // --> [[object Object], [object Object]]
 } ... });

然后第二个允许我通过el.weights[0].weight

这是猫鼬设计的差异还是我在我的第一个代码块中做错了什么来访问数组中的对象?

架构/数据的示例如下:

{ _id: 0,
    spname: 'INDV_748',
    weights:
     [ { weight: 1.463179736705023, diet: 'ln' },
       { weight: 11.78273309957772, diet: 'lgt' } ] }

如果我没有在 中指定_id: Numbernew mongoose.Schema({...})那么对于上述两种情况,_id都不会出现在任何find()查询中。

这也是设计的一部分吗?

4

0 回答 0