I'm developing an REST API for mobile app, This is part of model i'm using
我正在为移动应用程序开发REST API,这是我正在使用的模型的一部分
var UserSchema = new Schema(
{
email :
{
type : String,
unique : true
},
login_type : String,
username :
{
type : String,
index : true
},
password : String
}
And I added user's oid to post schema as reference
我添加了用户的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,
}
so, the posts can reference user collections to get username
of user who posted it. So i want to get just username of user, others are not necessary. For this, i used follows.
所以,帖子可以引用用户集合来获取发布它的用户的用户名。所以我想获得用户的用户名,其他人则没有必要。为此,我用了以下。
var query = Post.find({"regDate" :{$lte : before}},
{
_id : 1,
title : 1,
"author.username" : 1,
regDate : 1
})
but the mongoose ignored that "author.username"
. How can i get just username of author
, not all?
但是猫鼬忽略了“author.username”。我怎样才能获得作者的用户名,而不是全部?
1 个解决方案
#1
1
You should use populate
function and enumerate selected fields there:
你应该使用populate函数并枚举那里的选定字段:
Post
.find({ regDate :{ $lte : before }}, 'title reqDate')
.populate('author', 'username')
.exec();
#1
1
You should use populate
function and enumerate selected fields there:
你应该使用populate函数并枚举那里的选定字段:
Post
.find({ regDate :{ $lte : before }}, 'title reqDate')
.populate('author', 'username')
.exec();