반응형

app.js

var mongoose = require('mongoose');

const { Post } = require("./models")

mongoose.connect("mongodb://localhost:27017/myapp")

mongoose.connection.on("connected", () => {
    console.log("연결 완료");
})

mongoose.connection.on("disconnected", () => {
    console.log("연결 끊김");
})


async function main() {
    await Post.create({
        title: "제목3",
        content: "내용3",
        etc: "etc",
    })
}

main();


async function findList() {
    return await Post.find({})
}

findList().then((res) => {
    console.log(res.map((item) => { return item.title === '제목' }));
})

async function findItem() {
    return await Post.find({ title: "제목2" });
}

findItem().then((res) => {
    console.log(res);
})


async function changeitem() {
    res = await Post.findOneAndUpdate({
        id: '62d4e1b3d6408bbd8909a939',
        title: '12345'
    });

    console.log(res);
}

changeitem();



(async function deleteFun() {
    res = await Post.deleteOne({
        id: "62d4e1e2382904f6f3402a68",
    })
    console.log(res);
})()

 

models/schemas/board.js

const { Schema } = require("mongoose")

const PostSchema = new Schema({
    title: String,
    content: String,
    etc: String,
},
    {
        timestamps: true
    });

module.exports = PostSchema;

 

models/index.js

const mongoose = require("mongoose")
const PostSchema = require("./schemas/board")

exports.Post = mongoose.model("Post", PostSchema);

+ Recent posts