Last Sync: 2022-08-13 10:00:04

This commit is contained in:
tactonbishop 2022-08-13 10:00:04 +01:00
parent 6a330e2451
commit 2245bd7304

View file

@ -0,0 +1,33 @@
---
tags:
- Databases
- mongo_db
- node-js
- mongoose
---
# MongoDB connection, set-up and data query: complete example
```js
const mongoose = require("mongoose");
mongoose..connect("mongodb://127.0.0.1/[databse_name]");
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
data: Date,
isPublished: Boolean,
price: Number
})
const Course = mongoose.model("Course", courseSchema);
async function getCourses(){
return await Course
.find({isPublished: true, tags: "backend"})
.sort({name: 1})
.select({name: 1, author: 1});
}
```