2014-02-09 159 views

回答

2

除非您使用聚合框架,否则连接值最好在客户端上进行处理。

最简单的是使用使用Mongoose虚拟,或者你可以在你的Node.JS JavaScript代码中根据需要在客户端进行必要的连接。 (要查找具体文档,请转至this页面并搜索“虚拟”)。

该网页上的例子基本上是你想要什么:

var personSchema = new Schema({ 
    name: { 
    first: String, 
    last: String 
    } 
}); 

// compile our model 
var Person = mongoose.model('Person', personSchema); 

// create a document 
var bad = new Person({ 
    name: { first: 'Walter', last: 'White' } 
}); 

然后,添加一个虚拟:

personSchema.virtual('name.full').get(function() { 
    return this.name.first + ' ' + this.name.last; 
}); 

您可以通过只选择必要fields限制的结果以及执行级联(通过使用空格分隔的要从数据库返回的字段列表来指定):

Person.find({last: 'White'}, 'first last').exec(/* callback *); 
相关问题