我执行以下操作:
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
} ... });
但是当我建议像这样的Schema时:
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
这是通过mongoose设计的差异还是我在第一个代码块中做错了什么来访问数组中的对象?
架构/数据的示例如下:
{ _id: 0,
spname: 'INDV_748',
weights:
[ { weight: 1.463179736705023, diet: 'ln' },
{ weight: 11.78273309957772, diet: 'lgt' } ] }
如果我未在_id: Number
中指定new mongoose.Schema({...})
,则_id
将不会出现在上述两种情况的任何find()
查询中。
这也是设计的一部分吗?