├── .gitignore ├── src ├── models │ ├── index.js │ ├── Tag.js │ └── Tutorial.js └── server.js ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json -------------------------------------------------------------------------------- /src/models/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | Tag: require("./Tag"), 3 | Tutorial: require("./Tutorial") 4 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MongoDB - Mongoose One-to-Many relationship example 2 | 3 | For more detail, please visit: 4 | > [MongoDB Many-to-Many relationship tutorial with Mongoose examples](https://bezkoder.com/mongodb-many-to-many-mongoose/) 5 | 6 | ## Project setup 7 | ``` 8 | npm install 9 | ``` 10 | 11 | ### Run 12 | ``` 13 | node src/server.js 14 | ``` 15 | -------------------------------------------------------------------------------- /src/models/Tag.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | const Tag = mongoose.model( 4 | "Tag", 5 | new mongoose.Schema({ 6 | name: String, 7 | slug: String, 8 | tutorials: [ 9 | { 10 | type: mongoose.Schema.Types.ObjectId, 11 | ref: "Tutorial" 12 | } 13 | ] 14 | }) 15 | ); 16 | 17 | module.exports = Tag; 18 | -------------------------------------------------------------------------------- /src/models/Tutorial.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | const Tutorial = mongoose.model( 4 | "Tutorial", 5 | new mongoose.Schema({ 6 | title: String, 7 | author: String, 8 | tags: [ 9 | { 10 | type: mongoose.Schema.Types.ObjectId, 11 | ref: "Tag" 12 | } 13 | ] 14 | }) 15 | ); 16 | 17 | module.exports = Tutorial; 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-js-mongoose-many-to-many", 3 | "version": "1.0.0", 4 | "description": "Mongoose many to many relationship example", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "mongoose", 11 | "node", 12 | "js", 13 | "many", 14 | "to", 15 | "many", 16 | "mongodb" 17 | ], 18 | "author": "bezkoder", 19 | "license": "ISC", 20 | "dependencies": { 21 | "mongoose": "^5.8.11" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/server.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | const db = require("./models"); 3 | 4 | const createTutorial = function(tutorial) { 5 | return db.Tutorial.create(tutorial).then(docTutorial => { 6 | console.log("\n>> Created Tutorial:\n", docTutorial); 7 | return docTutorial; 8 | }); 9 | }; 10 | 11 | const createTag = function(tag) { 12 | return db.Tag.create(tag).then(docTag => { 13 | console.log("\n>> Created Tag:\n", docTag); 14 | return docTag; 15 | }); 16 | }; 17 | 18 | const addTagToTutorial = function(tutorialId, tag) { 19 | return db.Tutorial.findByIdAndUpdate( 20 | tutorialId, 21 | { $push: { tags: tag._id } }, 22 | { new: true, useFindAndModify: false } 23 | ); 24 | }; 25 | 26 | const addTutorialToTag = function(tagId, tutorial) { 27 | return db.Tag.findByIdAndUpdate( 28 | tagId, 29 | { $push: { tutorials: tutorial._id } }, 30 | { new: true, useFindAndModify: false } 31 | ); 32 | }; 33 | 34 | const getTutorialWithPopulate = function(id) { 35 | // return db.Tutorial.findById(id).populate("tags"); 36 | return db.Tutorial.findById(id).populate("tags", "-_id -__v -tutorials"); 37 | }; 38 | 39 | const getTagWithPopulate = function(id) { 40 | // return db.Tag.findById(id).populate("tutorials"); 41 | return db.Tag.findById(id).populate("tutorials", "-_id -__v -tags"); 42 | }; 43 | 44 | const run = async function() { 45 | var tut1 = await createTutorial({ 46 | title: "Tut #1", 47 | author: "bezkoder" 48 | }); 49 | 50 | var tagA = await createTag({ 51 | name: "tagA", 52 | slug: "tag-a" 53 | }); 54 | 55 | var tagB = await createTag({ 56 | name: "tagB", 57 | slug: "tag-b" 58 | }); 59 | 60 | var tutorial = await addTagToTutorial(tut1._id, tagA); 61 | console.log("\n>> tut1:\n", tutorial); 62 | 63 | var tag = await addTutorialToTag(tagA._id, tut1); 64 | console.log("\n>> tagA:\n", tag); 65 | 66 | tutorial = await addTagToTutorial(tut1._id, tagB); 67 | console.log("\n>> tut1:\n", tutorial); 68 | 69 | tag = await addTutorialToTag(tagB._id, tut1); 70 | console.log("\n>> tagB:\n", tag); 71 | 72 | var tut2 = await createTutorial({ 73 | title: "Tut #2", 74 | author: "zkoder" 75 | }); 76 | 77 | tutorial = await addTagToTutorial(tut2._id, tagB); 78 | console.log("\n>> tut2:\n", tutorial); 79 | 80 | tag = await addTutorialToTag(tagB._id, tut2); 81 | console.log("\n>> tagB:\n", tag); 82 | 83 | tutorial = await getTutorialWithPopulate(tut1._id); 84 | console.log("\n>> populated tut1:\n", tutorial); 85 | 86 | tag = await getTagWithPopulate(tag._id); 87 | console.log("\n>> populated tagB:\n", tag); 88 | }; 89 | 90 | mongoose 91 | .connect("mongodb://localhost/bezkoder_db", { 92 | useNewUrlParser: true, 93 | useUnifiedTopology: true 94 | }) 95 | .then(() => console.log("Successfully connect to MongoDB.")) 96 | .catch(err => console.error("Connection error", err)); 97 | 98 | run(); 99 | --------------------------------------------------------------------------------