1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| const mongoose = require("mongoose");
const DB_URL = "mongodb://localhost:27017/mydb";
mongoose.connect(DB_URL).then( () => { console.log("数据库连接成功"); }, (err) => { console.log("数据库连接失败"); } );
let schema = new mongoose.Schema({ title: String, content: String, time: String, });
const blogs = mongoose.model("blogs", schema, "blogs");
blogs.find({}, function (err, res) { console.log(res); });
const postdata = new blogs({ title: "post 2022", content: "hello here", time: "20220326", }); postdata.save();
blogs.insertMany( { title: "post 2023", content: "hello here", time: "20230326" }, function (err, rs) { console.log("保存成功:\n", rs); } );
blogs.deleteOne({ _id: "623de66ed61786a82df60832" }, function (err, rs) { console.log("已经删除一条数据:\n", rs); });
blogs.deleteMany({ title: "post 2023" }, function (err, res) { console.log("已经删除符合条件的数据:\n", res); });
|