现在有两张表
/*消息系统模型 */
information_model : {
name : "information_model",
cols : {
INFO_ID: String, //消息ID
INFO_TITLE : String, //标题
INFO_CONTENT : String, //消息内容
INFO_TIME : Date, //消息时间
INFO_TYPE : String //消息类型(消息--1、信息--2、短信--3、状态--4、工作--5)
}
},
/*消息状态模型*/
information_state : {
name : "information_state",
cols : {
INFO_ID: String, //消息ID
LOGIN_NAME: String, //用户登录名
INFO_STATE: String, //状态(未读--0、已读--1)
INFO_DELETE: String //是否删除(未删除--0、已删除--1)
}
},
两表INFO_ID是相同的。
现在需要联表查询(即返回值中包括information_model中的所有字段,而且包括 information_state中的INFO_STATE字段) 该如何操作?
最好是 mongoose的示例, 感谢。!
mongoose 可以关联数据的.
information_model 表加一个
information_state_info
Schema 里增加:information_state_info: {type:ObjectId, ref: "information_state" , index: true}
find 的时候
infoModel.find().populate('information_state_info',"INFO_ID LOGIN_NAME INFO_STATE INFO_DELETE").sort({INFO_ID: 1}).exec next
你的 information_state_info 就会出现对应数据.
但是每个数据都需要存储对应的 id.