mongoose 之 population 關聯查詢

本文分享如何經過 mongoose 的 population 實現關聯表查詢, population 的 api 可參考這裏
本文demo源碼.
1.初始化兩個schema和model。html

/**
 *  department schema
 **/
var departmentSchema = new Schema({
    id: String, //部門編號
    name: String //名稱
});


/**
 *  employee schema
 **/
var employeeSchema = new Schema({
    id: Number, //工號
    name: String, //姓名
    sex: Number, //性別
    age: Number, //年齡
    dep: {
        type: Schema.Types.ObjectId,
        ref: 'department'
    }
});

/**
 *  model
 **/
var employee = mongodb.mongoose.model("employee", employeeSchema);
var department = mongodb.mongoose.model('department', departmentSchema);

employee 的屬性 dep,對應model department. ref 表示關聯 department model.
被關聯的 model 的 type 必須是 ObjectId, Number, String, 和 Buffer.
2.初始化數據。git

//部門表
 db.departments.insert({"id":"1001","name":"市場部"});
 db.departments.insert({"id":"1002","name":"銷售部"});
 db.departments.insert({"id":"1003","name":"行政部"});

 //職工表
 db.employees.insert({"id":"001","name":"jack","sex":1,"age":23,"dep":"593667c4ec59b38aefdc25b5"});
 db.employees.insert({"id":"002","name":"tom","sex":2,"age":23,"dep":"593667c4ec59b38aefdc25b6"});
 db.employees.insert({"id":"003","name":"kan","sex":1,"age":43,"dep":"593667c4ec59b38aefdc25b5"});
 db.employees.insert({"id":"004","name":"julis","sex":2,"age":23,"dep":"593667c4ec59b38aefdc25b7"});
 db.employees.insert({"id":"005","name":"michael","sex":1,"age":43,"dep":"593667c4ec59b38aefdc25b5"});
 db.employees.insert({"id":"006","name":"jordan","sex":1,"age":25,"dep":"593667c4ec59b38aefdc25b5"});

注意此處的數據初始化順序。必須先初始化部門表,而後初始化員工表。
3.使用population關聯查詢github

employee.find({})    //查詢全部的employees表中的數據,並將結果返回給populate
         .populate({ path: 'dep', select: { name: 1 } })   //上述結果集合中的dep字段用departments表中的name字段填充
         .exec(function(err, obj) {
             callback(err, obj);
         });

3.查詢結果展現mongodb

渲染模版(ejs語法)及渲染語句:express

res.render('list', { employeeList: obj });//obj爲第三部中查詢結果
<table class="table table-bordered tab-content-center">
                <thead>
                    <tr>
                        <th>職工ID</th>
                        <th>姓名</th>
                        <th>性別</th>
                        <th>年齡</th>
                        <th>部門</th>
                    </tr>
                </thead>
                <tbody>
                    <% for(var i=0; i<employeeList.length; i++) {%>  
                        <tr>
                            <td><%= employeeList[i]._doc.id %></td>
                            <td><%= employeeList[i]._doc.name %></td>
                            <td>
                            <% if(employeeList[i]._doc.sex == 1) { %>
                                男
                            <% } else { %>
                                女
                            <% } %>
                            </td>
                            <td><%= employeeList[i]._doc.age %></td>
                            <td><%= employeeList[i]._doc.dep.name %></td>
                        </tr>
                    <% } %>  
                </tbody>
            </table>

圖片描述

相關文章
相關標籤/搜索