-
Notifications
You must be signed in to change notification settings - Fork 52
DOCSP-48893 Add get started with Mongoose tutorial #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stephmarie17
wants to merge
15
commits into
mongodb:master
Choose a base branch
from
stephmarie17:docsp-48893-mongooseGetStarted
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+958
−0
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
639b47a
add get started guide and code snippets
stephmarie17 eb39e54
edits
stephmarie17 1d1dd8a
vale fixes and add to landing page integrations section
stephmarie17 4129b7d
edits
stephmarie17 098022e
update title
stephmarie17 df13c4a
update section heading hierarchy
stephmarie17 dfe6995
steps test
rachel-mack 53f0b82
title test
rachel-mack 8cf7604
step formatting
rachel-mack 2a4b6b7
JS feedback
rachel-mack 55005b4
formatting
rachel-mack 61fd0aa
JS feedback and formatting
rachel-mack 5cc7f32
titles
rachel-mack c88384f
middleware updates
rachel-mack f7c3c23
JS feedback
rachel-mack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
source/includes/integrations/mongoose-blogSchema-validate.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import mongoose from 'mongoose'; | ||
const { Schema, SchemaTypes, model } = mongoose; | ||
|
||
// start-blogSchema | ||
const blogSchema = new Schema({ | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
slug: { | ||
type: String, | ||
required: true, | ||
lowercase: true, | ||
}, | ||
published: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
author: { | ||
type: String, | ||
required: true, | ||
}, | ||
content: String, | ||
tags: [String], | ||
createdAt: { | ||
type: Date, | ||
default: () => Date.now(), | ||
immutable: true, | ||
}, | ||
updated: Date, | ||
comments: [{ | ||
user: String, | ||
content: String, | ||
votes: Number | ||
}] | ||
}); | ||
// end-blogSchema | ||
|
||
// start-blogSchema-middleware | ||
blogSchema.pre('save', function(next) { | ||
this.updated = Date.now(); | ||
next(); | ||
}); | ||
// end-blogSchema-middleware | ||
|
||
const Blog = model('Blog', blogSchema); | ||
export default Blog; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from 'mongoose'; | ||
const { Schema, model } = mongoose; | ||
|
||
const blogSchema = new Schema({ | ||
title: String, | ||
slug: String, | ||
published: Boolean, | ||
author: String, | ||
content: String, | ||
tags: [String], | ||
createdAt: Date, | ||
updated: Date, | ||
comments: [{ | ||
user: String, | ||
content: String, | ||
votes: Number | ||
}] | ||
}); | ||
|
||
const Blog = model('Blog', blogSchema); | ||
export default Blog; |
63 changes: 63 additions & 0 deletions
63
source/includes/integrations/mongoose-get-started-blogSchema.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import mongoose from 'mongoose'; | ||
// start-blogSchema-import | ||
import Blog from './model/Blog.js'; | ||
// end-blogSchema-import | ||
|
||
mongoose.connect("<connection string>"); | ||
|
||
// start-insert | ||
// Creates a new blog post and inserts it into database | ||
const article = await Blog.create({ | ||
title: 'Awesome Post!', | ||
slug: 'awesome-post', | ||
published: true, | ||
content: 'This is the best post ever', | ||
tags: ['featured', 'announcement'], | ||
}); | ||
|
||
console.log('Created article:', article); | ||
// end-insert | ||
|
||
// start-update | ||
// Updates the title of the article | ||
article.title = "The Most Awesomest Post!!"; | ||
await article.save(); | ||
console.log('Updated Article:', article); | ||
// end-update | ||
|
||
// start-find-by-id | ||
// Finds the article by its ID. Replace <object id> with the objectId of the article. | ||
const articleFound = await Blog.findById("<object id>").exec(); | ||
console.log('Found Article by ID:', articleFound); | ||
// end-find-by-id | ||
|
||
// start-project-fields | ||
// Finds the article by its ID and projects only the title, slug, and content fields. | ||
// Replace <object id> with the objectId of the article. | ||
const articleProject = await Blog.findById("<object id>", "title slug content").exec(); | ||
console.log('Projected Article:', articleProject); | ||
// end-project-fields | ||
|
||
// start-delete-one | ||
// Deletes one article with the slug "awesome-post". | ||
const blogOne = await Blog.deleteOne({ slug: "awesome-post" }); | ||
console.log('Deleted One Blog:', blogOne); | ||
// end-delete-one | ||
|
||
// start-delete-many | ||
// Deletes all articles with the slug "awesome-post". | ||
const blogMany = await Blog.deleteMany({ slug: "awesome-post" }); | ||
console.log('Deleted Many Blogs:', blogMany); | ||
// end-delete-many | ||
|
||
// start-validated-insert | ||
// Creates a new blog post and inserts it into database | ||
const article = await Blog.create({ | ||
title: 'Awesome Post!', | ||
slug: 'awesome-post', | ||
published: true, | ||
author: 'A.B. Cee', | ||
content: 'This is the best post ever', | ||
tags: ['featured', 'announcement'], | ||
}); | ||
// end-validated-insert |
53 changes: 53 additions & 0 deletions
53
source/includes/integrations/mongoose-get-started-multiple-schemas.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import mongoose from 'mongoose'; | ||
import Blog from './model/Blog.js'; | ||
// start-user-import | ||
import User from './model/User.js'; | ||
// end-user-import | ||
|
||
mongoose.connect("<connection string>"); | ||
|
||
// start-create-user | ||
// Create a new user | ||
const user = await User.create({ | ||
name: 'Jess Garica', | ||
email: '[email protected]', | ||
}); | ||
// end-create-user | ||
|
||
// start-article-with-author | ||
// Creates a new blog post that references the user as the author | ||
const articleAuthor = await Blog.create({ | ||
title: 'Awesome Post!', | ||
slug: 'Awesome-Post', | ||
author: user._id, | ||
content: 'This is the best post ever', | ||
tags: ['featured', 'announcement'], | ||
}); | ||
|
||
console.log('Article with Author:', articleAuthor); | ||
// end-article-with-author | ||
|
||
// start-populate-author | ||
// Populates the author field with user data | ||
const articlePopulate = await Blog.findOne({ title: "Awesome Post!" }).populate("author"); | ||
console.log('Article Populated:', articlePopulate); | ||
// end-populate-author | ||
|
||
// start-middleware-update | ||
// Uses middleware to update the updated field before saving and updating | ||
|
||
// Create a new article | ||
const articleMiddlewareUpdate = await Blog.create({ | ||
title: 'Another Awesome Post!', | ||
slug: 'Another-Awesome-Post', | ||
author: user._id, | ||
content: 'Here is another awesome post', | ||
}); | ||
console.log('Original Article: ', articleMiddlewareUpdate); | ||
// Wait | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
// Update the article | ||
articleMiddlewareUpdate.content = "Check my updated field" | ||
await articleMiddlewareUpdate.save(); | ||
console.log('Auto-updated Article:', articleMiddlewareUpdate); | ||
// end-middleware-update |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const Blog = mongoose.model('Blog', blog); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from 'mongoose'; | ||
const { Schema, model } = mongoose; | ||
// start-schema-example | ||
const blog = new Schema({ | ||
title: String, | ||
slug: String, | ||
published: Boolean, | ||
author: String, | ||
content: String, | ||
tags: [String], | ||
createdAt: Date, | ||
updated: Date, | ||
comments: [{ | ||
user: String, | ||
content: String, | ||
votes: Number | ||
}] | ||
}); | ||
// end-schema-example | ||
const Blog = model('Blog', blog); | ||
export default Blog; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mongoose from 'mongoose'; | ||
const {Schema, model} = mongoose; | ||
|
||
const userSchema = new Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
email: { | ||
type: String, | ||
minLength: 10, | ||
required: true, | ||
lowercase: true | ||
}, | ||
}); | ||
|
||
const User = model('User', userSchema); | ||
export default User; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.. _node-integrations: | ||
|
||
================================== | ||
Third-Party Integrations and Tools | ||
================================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: i | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
:maxdepth: 1 | ||
|
||
Get Started with Mongoose </integrations/mongoose-get-started> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We typically recommend using Mongoose's
timestamps
option rather than writing your own createdAt/updatedAt timestamps, because Mongoose's timestamps also handle updating timestamps when usingupdateOne()
, etc.