Mongoose 多表(N個表)關聯查詢概述mongodb
需求:文章(article),文章分類(articlecate),用戶(user)這三個表之間的關係,一篇文章對應文章分類表中的某個類型,對應着用戶表中的某個用戶數據庫
Mongoose 多表(N個表)關聯查詢代碼實現app
首先咱們須要建立db.js(鏈接數據庫)的模塊mongoose
var mongoose = require('mongoose'); mongoose.connect('mongodb://127.0.0.1:27017/eggcms',{ useNewUrlParser: true },function(err){ if(err){ console.log(err); return; } console.log('數據庫鏈接成功') }); module.exports = mongoose;
經過上面的需求咱們須要創建三個model模塊分別是article.js,articlecate.js,user.jsui
var mongoose = require('./db.js'); var Schema = mongoose.Schema; var ArticleSchema = new Schema({ title: { type: String, unique: true }, cid: { // 分類id type: Schema.Types.ObjectId }, author_id:{ // 用戶的id type: Schema.Types.ObjectId }, author_name:{ type:String }, descripton: String, content: String }); module.exports = mongoose.model('Article',ArticleSchema,'article');
var mongoose = require('./db.js'); var ArticleCateSchema = new mongoose.Schema({ title : { type: String, unique: true }, descripton: String, addtime: { type: Date } }); module.exports = mongoose.model('ArticleCate',ArticleCateSchema,'articlecate');
var mongoose = require('./db.js'); var UserSchema = new mongoose.Schema({ username: { type: String, unique: true }, password: String, name: String, age: Number, sex: String, tel: Number, status: { type: Number, default: 1 } }); module.exports = mongoose.model('User',UserSchema,'user');
而後分別對應三個添加數據的模塊article_add.js,articlecate_add.js,user_add.jsspa
var ArticleModel = require('./model/article.js'); var article = new ArticleModel(); article.title="這是一個國際新聞333333333" article.cid='5cde87e21ebf22597c973f1f'; // 分類id article.author_id='5cde88f71faa8045e439838b'; // 用戶id article.author_name='李四'; article.descripton='這是一個國際新聞333333333333 此處省略300字'; article.content='訪問美國 這是一個國際新聞333333333' article.save();
var ArticleCateModel = require('./model/articlecate.js'); // 分類的增長 var cate = new ArticleCateModel({ title:'地方新聞', description:'地方新聞' }) cate.save();
var UserModel = require('./model/user.js'); // 增長用戶 var user= new UserModel({ username :'wangwu', password:'qwerqwerqewrq', name:'王五', age:21, sex:'男', tel:12345678987 }) user.save();
當咱們添加了一些數據以後再app.js中進行數據庫查詢的操做code
查詢文章信息blog
var ArticleModel = require('./model/article.js') // 查詢文章信息 ArticleModel.find({}, (err,docs) => { console.log(docs) })
兩個表關聯查詢ip
var ArticleModel = require('./model/article.js'); //兩個表關聯查詢 ArticleModel.aggregate([ { $lookup: { from: "articlecate", localField: "cid", foreignField: "_id", as: "cate" } } ],function(err,docs){ console.log(JSON.stringify(docs)) })
將其轉成正常的JSON便於查看效果ci
多表關聯查詢
查詢文章信息 並顯示文章的分類 以及文章的做者信息
var ArticleModel = require('./model/article.js'); // 查詢文章信息 並顯示文章的分類 以及文章的做者信息 // 三個表關聯查詢 ArticleModel.aggregate([ { $lookup: { from: "articlecate", localField: "cid", foreignField: "_id", as: "cate" } }, { $lookup: { from: "user", localField: "author_id", foreignField: "_id", as: "user" } } ],function(err,docs){ console.log(JSON.stringify(docs)); })
將其轉成正常的JSON便於查看效果
[{ "_id": "5cde89b2ecdd5e4864bdaa44", "title": "這是一個國內新聞11111111", "cid": "5cde87ce5ac6d7551041c568", "author_id": "5cde8913a0e31553449bdf54", "author_name": "王五", "descripton": "這是一個國內新聞11111111 此處省略300字", "content": "訪問美國 這是一個國內新聞11111111", "__v": 0, "cate": [{ "_id": "5cde87ce5ac6d7551041c568", "title": "國內新聞", "__v": 0 }], "user": [{ "_id": "5cde8913a0e31553449bdf54", "status": 1, "username": "wangwu", "password": "qwerqwerqewrq", "name": "王五", "age": 21, "sex": "男", "tel": 12345678987, "__v": 0 }] }, { "_id": "5cde89dc0d116f45fca7f559", "title": "這是一個國際新聞222222222222", "cid": "5cde87e21ebf22597c973f1f", "author_id": "5cde8913a0e31553449bdf54", "author_name": "王五", "descripton": "這是一個國際新聞222222222222 此處省略300字", "content": "訪問美國 這是一個國際新聞222222222222", "__v": 0, "cate": [{ "_id": "5cde87e21ebf22597c973f1f", "title": "國際新聞", "__v": 0 }], "user": [{ "_id": "5cde8913a0e31553449bdf54", "status": 1, "username": "wangwu", "password": "qwerqwerqewrq", "name": "王五", "age": 21, "sex": "男", "tel": 12345678987, "__v": 0 }] }, { "_id": "5cde8a078a1ab250f4dbb8e0", "title": "這是一個國際新聞333333333", "cid": "5cde87e21ebf22597c973f1f", "author_id": "5cde88f71faa8045e439838b", "author_name": "李四", "descripton": "這是一個國際新聞333333333333 此處省略300字", "content": "訪問美國 這是一個國際新聞333333333", "__v": 0, "cate": [{ "_id": "5cde87e21ebf22597c973f1f", "title": "國際新聞", "__v": 0 }], "user": [{ "_id": "5cde88f71faa8045e439838b", "status": 1, "username": "lisi", "password": "13214lkisisgfdsgsdsg", "name": "李四", "age": 20, "sex": "男", "tel": 124212142151, "__v": 0 }] }]