我希望使用Sails js和MongoDB的.populate()方法,所以我做了以下操作:
我正在使用最新版本的Sails、MongoDB和Node。
我的模型如下:
// User.js
module.exports = {
attributes: {
name: {
type: 'string'
},
pets: {
collection: 'pet',
via: 'owner'
}
}
};
// Pet.js
module.exports = {
attributes: {
name: {
type: 'string'
},
owner: {
model: 'user'
}
}
};
我使用了与文档相同的以下内容:
User.find()
.populate('pets')
.exec(function(err, users) {
if(err) return res.serverError(err);
res.json(users);
});
但是我得到了以下输出:
[
{
"pets": [],
"id": "58889ce5959841098d186b5a",
"firstName": "Foo",
"lastName": "Bar"
}
]
migrate: 'safe'
。 - Craig van Tonder