我有2个架构:
var siteFeedbackSchema = new Schema({
user: {type:Schema.ObjectId, ref: constants.DB_MODELS.USERS, required: true},
comment: {type: String},
__sv: {type: Number, default: SCHEMA_VERSION}
}, {strict: true, timestamps: { createdAt: 'created', updatedAt: 'modified'}});
var usersSchema = new Schema({
userName: {type: String, required: true},
firstName: {type: String, required: true},
lastName: {type: String, required: true},
__sv: {type: Number, default: SCHEMA_VERSION}
}, {strict: true, timestamps: { createdAt: 'created', updatedAt: 'modified'}});
我填充用户密钥,当我在/ sitefeedbackSchema上执行GET并返回doc:
{
"_id": "57850bd22e08cf206a3064e3"
"modified": "2016-07-12T15:25:06.652Z"
"created": "2016-07-12T15:25:06.652Z"
"comment": "good"
"user": {
"_id": "5783cdcc20de829c5613a9a1"
"modified": "2016-07-11T16:48:12.371Z"
"created": "2016-07-11T16:48:12.371Z"
"userName": "ramanr"
"firstName": "rads"
"lastName": "rrrr"
"__v": 0
"__sv": 1
}
"__v": 0
"__sv": 1
}
但是,我只希望从用户架构中显示userName。有没有办法选择我们想要的键?
{
"_id": "57850bd22e08cf206a3064e3"
"modified": "2016-07-12T15:25:06.652Z"
"created": "2016-07-12T15:25:06.652Z"
"comment": "good"
"user": {
"userName": "ramanr"
}
"__v": 0
"__sv": 1
}
是否有支持这个的cli?
答案 0 :(得分:0)
看看预测。基本上MongoDB已经构建为每次检索完整的文档,但您可以使用投影来避免这种限制: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
答案 1 :(得分:0)
如果您只想为填充的文档返回一些特定字段?这可以通过将通常的字段名称语法作为第二个参数传递给populate方法来实现。我假设你使用mongoose所以你可以使用像
fruit.Length >= index && fruit != "banana"