这是用户架构
const UsersSchema = Schema({
name: String,
email: {type: String, unique: true, lowercase: true},
password: {type: String, select: false},
created: {type: Date, default: Date.now()},
});
module.exports = mongoose.model('User', UsersSchema);
这是图片架构
const ImagesSchema = Schema({
image: String,
userId: {type: String, select: false},
created: {type: Date, default: Date.now()}
});
module.exports = mongoose.model('Image', ImagesSchema);
这是查询
ImageSchema.aggregate()
.lookup(
{ from: 'users', localField: 'userId', foreignField: '_id', as: 'user' }
)
.exec( (err, images) => {
console.log(err,images);
})
始终使用用户属性返回图像,如空数组
目前的行为是什么?
{ _id: 5b0ed0a671dd9a3c7ac115f5,
image: 'testigimage.png',
userId: '5aa97c526fc4e7108738e8c1',
__v: 0,
user: [] } ]
Node.js,mongoose和MongoDB版本。
节点v9.11.1 猫鼬v ^ 5.0.10 Mongo v3.4.13