Quelle: https://www.treefrogframework.org/en/user-guide/model/object-document-mapping-on-mongodb.html
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
username: {
type: String,
trim: true,
unique: true,
required: [true, 'Username is required']
},
password: {
type: String,
required: [true, 'Password is required'],
},
created: {
type: Date,
default: Date.now()
}
});
module.exports = mongoose.model('User', UserSchema);
Quelle: https://mongoosejs.com/docs/guide.html
const Person = require("../models/person");
// .find() finds all instances in the database that match the query you pass in.
// It returns an array, even if there is only one item in the array.
// No query passed in means "find everything"
Person.find((err, people) => {
// Note that this error doesn't mean nothing was found,
// it means the database had an error while searching, hence the 500 status
if (err) return res.status(500).send(err)
// send the list of all people
return res.status(200).send(people);
});
// If query IS passed into .find(), filters by the query parameters
Person.find({name: "John James", age: 36}, (err, people) =>{
if (err) return res.status(500).send(err)
// send the list of all people in database with name of "John James" and age of 36
// Very possible this will be an array with just one Person object in it.
return res.status(200).send(people);
});
const Todo = require("../models/todo");
// Assuming this is from a POST request and the body of the
// request contained the JSON of the new "todo" item to be saved
const newTodoObj = new Todo(req.body);
newTodoObj.save(err => {
if (err) return res.status(500).send(err);
return res.status(200).send(newTodoObj);
});
const Todo = require("../models/todo");
// This would likely be inside of a PUT request, since we're updating an existing document, hence the req.params.todoId.
// Find the existing resource by ID
Todo.findByIdAndUpdate(
// the id of the item to find
req.params.todoId,
// the change to be made. Mongoose will smartly combine your existing
// document with this change, which allows for partial updates too
req.body,
// an option that asks mongoose to return the updated version
// of the document instead of the pre-updated one.
{new: true},
// the callback function
(err, todo) => {
// Handle any possible database errors
if (err) return res.status(500).send(err);
return res.send(todo);
}
)
// The "todo" in this callback function represents the document that was found.
// It allows you to pass a reference back to the client in case they need a reference for some reason.
Todo.findByIdAndRemove(req.params.todoId, (err, todo) => {
// As always, handle any potential errors:
if (err) return res.status(500).send(err);
// We'll create a simple object to send back with a message and the id of the document that was removed
// You can really do this however you want, though.
const response = {
message: "Todo successfully deleted",
id: todo._id
};
return res.status(200).send(response);
});
Quelle: https://coursework.vschool.io/mongoose-crud/
Quelle: https://www.mongodb.com/docs/manual/core/databases-and-collections/
Quelle: https://www.mongodb.com/compatibility/json-to-mongodb
Quelle: https://www.mongodb.com/de-de/cloud/atlas/security