2015-09-20 117 views
0

我有以下产品型号:猫鼬find()方法返回数组空

'use strict'; 

let mongoose = require('mongoose'); 
let Schema = mongoose.Schema; 

// create a schema 
let produtoSchema = new Schema(
    { 
     descricao: { type: String, required: true }, 
     gateways: [ { type : mongoose.Types.ObjectId, ref: 'Gateway' } ] 
    } 
); 

mongoose.model('Produto', produtoSchema); 

在以下集合:

rs0:PRIMARY> db.produtos.find().pretty() 
{ 
    "_id" : ObjectId("55fef1a3d7c6912033f2da72"), 
    "descricao" : "Product description", 
    "gateways" : [ 
     ObjectId("55fee8a97cb7db7740acb322") 
    ] 
} 
rs0:PRIMARY> 

所以,我试图用猫鼬获取特定产品但 '网关' 数组为空:

let Produto  = mongoose.model('Produto'); 
Produto.find(
{ 
    _id: mongoose.Types.ObjectId("55fef1a3d7c6912033f2da72") 

}, function(err, result) 
{ 
    if (err) console.log(err); 

    console.log(result); 
}); 

,其结果是:

[ { _id: 55fef1a3d7c6912033f2da72, 
    descricao: 'Product description', 
    gateways: [] } ] 

A也尝试过,但结果相同:

Produto 
.find({ _id: mongoose.Types.ObjectId("55fef1a3d7c6912033f2da72") }) 
.populate('gateways') 
.exec(function(err, result) 
{ 
    if (err) console.log(err); 
    console.log(result); 
}); 

什么我做错了任何想法?

谢谢。

+0

尝试使用'_id:“55fef1a3d7c6912033f2da72”' – shan1024

+0

同样的结果。我正在读取文档,唯一的问题是网关[]数组是空的而不是网关[ObjectId(“55fee8a97cb7db7740acb322”)] –

回答

0

我发现了这个问题。

我已经改变了:

gateways: [ { type : mongoose.Types.ObjectId, ref: 'Gateway' } ] 

通过

gateways: [ { type : mongoose.Schema.ObjectId, ref: 'Gateway' } ] 

什么是它们之间的区别?

+0

请参阅http://stackoverflow.com/a/28617920/1259510 – JohnnyHK