猫鼬“填充"不填充
我正在MEAN堆栈上构建一个相当简单的应用程序,我的确超出了我的深度,尤其是当涉及到猫鼬时.我发现猫鼬的文档很难缠住我的头,在其他任何地方都找不到答案.
I am building a fairly simple application on the MEAN stack and I am really out of my depth, especially when it comes to mongoose. I have found the mongoose documentation very difficult to wrap my head around and cannot find answers anywhere else.
我的问题是:我有一堆用户,这些用户有存储库,而存储库有存储库提供程序(GitHub,BitBucket等).
My issue is this: I have a bunch of users, these users have repositories and the repositories have repository providers (GitHub, BitBucket etc).
用户有很多存储库,而一个存储库只有一种存储库类型.
A user has many repositories and a repository has one repository type.
我的用户文件包含以下内容:
My user file contains the following:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
repositories: [{
name: String,
branches: [{
name: String,
head: Boolean,
commits: [{
hash: String,
date: Date,
message: String,
contributer: String,
avatar: String
}]
}],
repoType: {
type: Schema.Types.ObjectId,
ref: 'RepoProviders'
}
}]
});
var User = mongoose.model('User', UserSchema);
module.exports = User;
// This is where the magic doesn't happen :(
User.find({ name: "John Smith"}).populate({path: 'repoType'}).exec(function (err, user) {
if (err) return handleError(err);
console.log(user);
});
RepoProvider.js包含:
RepoProvider.js contains:
var mongoose = require('mongoose');
Schema = mongoose.Schema;
var RepoProviderSchema = new Schema({
name: {
type: String,
required: true
}
});
var RepoProvider = mongoose.model('RepoProviders', RepoProviderSchema);
module.exports = RepoProvider;
我正在mongo中创建用户文档,并手动分配一个repoType id哈希(取自现有的repoType文档).
I am creating user documents in mongo and manually assigning a repoType id hash (taken from an existing repoType document).
当我console.log用户时,回购类型设置为id,但是没有返回任何关系:
When I console.log the User, the repo type is set to the id but there is no relationship returned:
[ { _id: 5547433d322e0296a3c53a16,
email: 'john@smith.com',
name: 'John Smith',
__v: 0,
repositories:
[ { name: 'RepoOne',
repoType: 5547220cdd7eeb928659f3b8,
_id: 5547433d322e0296a3c53a17,
branches: [Object] } ] } ]
如何正确设置和查询此关系?
How do I properly set and query this relationship?