const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost: 27017/mongo-relation', { useNewUrlParser: true, useUnifiedTopology: true}, err => {if (err) {console.log('数据库连接失败');
}
console.log('数据库连接成功');
})
const categorySchema = new mongoose.Schema({
name: {type: String}
}, {toJSON: { virtuals: true}
});
categorySchema.virtual('posts', {
// 本地键
localField: '_id',
// 关联的模型
ref: 'Post',
// 外键
foreignField: 'categories',
// 输出多个
justOne: false
});
const Category = mongoose.model('Category', categorySchema);
const postSchema = new mongoose.Schema({
title: {type: String},
body: {type: String},
category: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'Category'
},
categories: [{
type: mongoose.SchemaTypes.ObjectId,
ref: 'Category'
}]
});
const Post = mongoose.model('Post', postSchema);
async function init() {// const cats = await Category.find().populate('categories');
// console.log(cats);
// [// { _id: 5ee0a7f22adf1624e8b9b64d, name: 'nodejs', __v: 0},
// {_id: 5ee0a7f22adf1624e8b9b64e, name: 'vue.js', __v: 0}
// ]
// 加上 lean()
// const cats = await Category.find().populate('posts').lean();
// console.log(cats);
// [
// {
// _id: 5ee0a7f22adf1624e8b9b64d,
// name: 'nodejs',
// __v: 0,
// posts: [[Object] ]
// },
// {
// _id: 5ee0a7f22adf1624e8b9b64e,
// name: 'vue.js',
// __v: 0,
// posts: [[Object], [Object] ]
// }
// ]
// cats[0].posts 查找第 1 个分类内容
// const cats = await Category.find().populate('posts').lean();
// console.log(cats[0].posts);
// [
// {
// _id: 5ee0a74dc6a921238454fa52,
// title: '第 2 篇文章',
// body: '内容 2',
// __v: 9,
// category: 5ee0a7f22adf1624e8b9b64d,
// categories: [5ee0a7f22adf1624e8b9b64d, 5ee0a7f22adf1624e8b9b64e]
// }
// ]
// JSON.stringify
const cats = await Category.find().populate('posts').lean();
console.log(JSON.stringify(cats));
// [{"_id":"5ee0a7f22adf1624e8b9b64d","name":"nodejs","__v":0,
// "posts":[{"_id":"5ee0a74dc6a921238454fa52",
// "title":"第 2 篇文章","body":"内容 2","__v":9,
// "category":"5ee0a7f22adf1624e8b9b64d",
// "categories":["5ee0a7f22adf1624e8b9b64d","5ee0a7f22adf1624e8b9b64e"]}]},
// {"_id":"5ee0a7f22adf1624e8b9b64e","name":"vue.js","__v":0,"posts":[{"_id":"5ee0a74dc6a921238454fa51","title":"第 1 篇文章","body":"内容 1","__v":5,"category":"5ee0a7f22adf1624e8b9b64d","categories":["5ee0a7f22adf1624e8b9b64e"]},{"_id":"5ee0a74dc6a921238454fa52","title":"第 2 篇文章","body":"内容 2","__v":9,"category":"5ee0a7f22adf1624e8b9b64d","categories":["5ee0a7f22adf1624e8b9b64d","5ee0a7f22adf1624e8b9b64e"]}]}]
// await Post.insertMany([{
// title: '第 1 篇文章',
// body: '内容 1'
// }, {
// title: '第 2 篇文章',
// body: '内容 2'
// }]);
// await Category.insertMany([{
// name: 'nodejs'
// },
// {
// name: 'vue.js'
// }
// ]);
// const category = await Category.find();
// console.log(category);
// const cat1 = await Category.findOne({name: 'nodejs'});
// const cat2 = await Category.findOne({name: 'vue.js'});
// const post1 = await Post.findOne({title: '第 1 篇文章'});
// const post2 = await Post.findOne({title: '第 2 篇文章'});
// posts.category
// console.log(cat1);
// post1.categories = [cat2];
// post2.categories = [cat1, cat2];
// await post1.save();
// await post2.save();
// const post = await Post.find();
// console.log(post);
// console.log(post1, post2);
// const post = await Post.find().populate('categories')
// console.log(post[0], post[1]);
}
init()