Last Sync: 2022-08-13 10:00:04
This commit is contained in:
parent
6a330e2451
commit
2245bd7304
1 changed files with 33 additions and 0 deletions
|
@ -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});
|
||||||
|
}
|
||||||
|
```
|
Loading…
Add table
Reference in a new issue