eolas/neuron/666a9949-2ad4-4228-a102-35f508a9db5d/MongoDB_connection.md
2025-02-11 18:11:49 +00:00

588 B

tags
mongo-db
node-js
mongoose
databases

MongoDB connection

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});
}