Skip to content

Instantly share code, notes, and snippets.

@abskmj
Last active July 19, 2022 09:28
Show Gist options
  • Save abskmj/cb5bb57829c677fe38b724ad864eaa5b to your computer and use it in GitHub Desktop.
Save abskmj/cb5bb57829c677fe38b724ad864eaa5b to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose')
const { MongoMemoryServer } = require('mongodb-memory-server')
const User = require('./user.model')
const Post = require('./post.model')
const start = async () => {
// connect to a mongo database
const mongodb = new MongoMemoryServer()
const connectionUri = await mongodb.getUri()
await mongoose.connect(
connectionUri, { useNewUrlParser: true, useUnifiedTopology: true }
)
// add seed data
const author = await User.create({
name: 'abskmj',
email: 'abskmj@email.com'
})
await Post.create([{
name: 'Relation: Belongs To',
author: author
}, {
name: 'Relation: Has One',
author: author
}])
//= ==== RELATION: Belongs To =====//
// posts without author
const posts = await Post.find({})
console.log(posts)
/*
{
_id: 5f71af94e66f4728f68ffc06,
name: 'Relation: Belongs To',
author: 5f71af94e66f4728f68ffc05
}
*/
// posts with author details pre populated
const postsWithAuthor = await Post.find({}).populate('author')
console.log(postsWithAuthor)
/*
{
_id: 5f71af94e66f4728f68ffc06,
name: 'Relation: Belongs To',
author: {
_id: 5f71af94e66f4728f68ffc05,
name: 'abskmj',
email: 'abskmj@email.com'
}
}
*/
//= ==== RELATION: Has Many / Has One =====//
// user without posts
const user = await User.findOne()
console.log(user)
/*
{
_id: 5f71ce43687f2a2fb97fa462,
name: 'abskmj',
email: 'abskmj@email.com',
id: '5f71ce43687f2a2fb97fa462'
}
*/
// user with posts prepopulated
const userWithPosts = await User.findOne().populate('posts')
console.log(userWithPosts)
/*
{
_id: 5f71ce43687f2a2fb97fa462,
name: 'abskmj',
email: 'abskmj@email.com',
posts: [
{
_id: 5f71ce43687f2a2fb97fa463,
name: 'Relation: Belongs To',
author: 5f71ce43687f2a2fb97fa462
},
{
_id: 5f71ce43687f2a2fb97fa464,
name: 'Relation: Has One',
author: 5f71ce43687f2a2fb97fa462
}
],
id: '5f71ce43687f2a2fb97fa462'
}
*/
// close mongo connection
await mongoose.connection.close()
await mongodb.stop()
}
start()
{
"name": "mongoose-model-relations",
"version": "1.0.0",
"description": "Model Relations in Mongoose",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "standard",
"start": "node .",
"dev": "nodemon --exec \"standard --fix && node . \""
},
"author": "abskmj@gmail.com",
"license": "MIT",
"dependencies": {
"mongodb-memory-server": "^6.8.0",
"mongoose": "^5.9.26",
"nodemon": "^2.0.4"
},
"devDependencies": {
"standard": "^14.3.4"
}
}
const mongoose = require('mongoose')
const { Schema } = mongoose
const schema = new Schema({
name: {
type: String,
required: true
},
author: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
}
}, { versionKey: false })
module.exports = mongoose.model('Post', schema)
const mongoose = require('mongoose')
const { Schema } = mongoose
const schema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
}
}, {
toObject: { virtuals: true },
toJSON: { virtuals: true },
versionKey: false
})
schema.virtual('posts', {
ref: 'Post',
localField: '_id',
foreignField: 'author'
})
module.exports = mongoose.model('User', schema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment