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()