我正在为移动应用开发REST API,这是我正在使用的模型的一部分
var UserSchema = new Schema(
{
email :
{
type : String,
unique : true
},
login_type : String,
username :
{
type : String,
index : true
},
password : String
}
并且我添加了用户的oid来发布架构作为参考
var PostSchema = new Schema(
{
author : {type : Schema.Types.ObjectId, ref : "User"},
board_id : {type : Schema.Types.ObjectId, ref : "Board"},
regDate : Number,
lastModDate : Number,
title : String,
content : String,
}
因此,帖子可以引用用户集合以获取username
发布该帖子的用户。因此,我只想获取用户的用户名,其他人则没有必要。为此,我使用了以下方法。
var query = Post.find({"regDate" :{$lte : before}},
{
_id : 1,
title : 1,
"author.username" : 1,
regDate : 1
})
但是猫鼬对此视而不见"author.username"
。我怎么能只获得的用户名author
,而不是全部?
您应该使用populate
function并枚举其中的选定字段:
Post
.find({ regDate :{ $lte : before }}, 'title reqDate')
.populate('author', 'username')
.exec();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句