我想按照以下描述获取nodejs应用程序中的数据。
user.js,userProfile是猫鼬的模型。猫鼬 - 从多个集合中获取数据
user.js的
var userSchema = new Schema({
nick_name:{type:String},
email: {type: String},
password: {type: String},
is_active:{type:String,enum:['1','0'],default:"1"},
},{ collection: 'user'});
userProfile.js
var userProfileSchema = new Schema({
user_id:{type:Schema.Types.ObjectId, ref:'User'},
first_name:{type:String},
last_name:{type:String},
address:{type:String},
country:{type:String},
state:{type:String},
city:{type:String},
gender:{type:String,enum:['m','f','o'],default:"m"},
is_active:{type:String,enum:['1','0'],default:"1"},
},{ collection: 'userProfile'});
想离开把如下
{
"nick_name" : "laxman",
"email" : "[email protected]",
"first_name" : "my first name",
"last_name" : "my last name",
"address" : "my address",
"country" : "my country",
"state" : "my state",
"city" : "my city",
"gender" : "m",
}
依赖
"express" => "version": "4.7.4",
"mongoose" => "version": "4.4.5",
"mongodb" => "version": "2.4.9",
"OS" => "ubuntu 14.04 lts 32bit",
SQL查询参考
SELECT * FROM `user`
left join `user_profile`
on user.id =user_profile.user_id
WHERE 1
你必须使用MongoDB的map-reduce功能。这里有一些ref链接。 [示例](https://www.noppanit.com/merge-documents-two-collections-together-mongodb) [StackOverflow](http:// stackoverflow。com/questions/5681851/mongodb-combine-data-from-multiple-collections-into-one-how) [MongoDB](https://docs.mongodb.org/manual/core/map-reduce/ #MapReduce-Outputoptions) – Sarju
如何在猫鼬中使用它 – laxman