可以直接改,不用$set
//使用mongose模块 cnpm install mongoose --save
//1引入模块
let mongoose = require("mongoose");
//2链接数据库
//then()方法前返回的是一个promise对象
mongoose.connect("mongodb://127.0.0.1:27017/student").then(() => {
console.log("链接数据库成功");
}).catch(err => {
console.log(err);
});
//3创建一个集合规则
let userSchema = new mongoose.Schema({
username: String,
age: Number,
sex: String
});
//4 利用规则创建集合
let Users = mongoose.model("Users", userSchema);
//5查询数据 查询所有数据
// Users.find().then(result => {
// console.log(result);
// })
// Users.find({
// age: 20
// }).then(result => {
// console.log(result);
// })
// Users.find({
// age: {
// $gt: 20
// }
// }).then(result => {
// console.log(result);
// })
// Users.find().sort({
// age: 1
// }).then(result => {
// console.log(result);
// })
// Users.find().sort({
// age: 1
// }).limit(3).skip(1).then(result => {
// console.log(result);
// });
//修改数据
// Users.updateOne({
// username: "小明"
// }, {
// $set: {
// age: 38
// }
// }).then(result => {
// console.log(result);
// })
//更新多条数据
// Users.updateMany({}, {
// username: "小小明"
// }).then(result => {
// console.log(result);
// })
//删除数据
// Users.findOneAndDelete({
// age: 38
// }).then(result => {
// console.log(result);
// })
// 删除多条数据
// Users.deleteMany({}).then(result => {
// console.log(result);
// })