const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', { useUnifiedTopology: true }).then(() => console.log('数据库连接成功')).catch(err => console.log(err, '数据库连接失败'))//创建集合规则
const courseSchema = new mongoose.Schema({name: String,age: Number,email: String,password: String,hobbies: [String]
});
//使用集合并应用规则
const User = mongoose.model('User', courseSchema);//查询用户里面得所有命令
//User.find().then(result => console.log(result));
//查询一条 数据不存在 返回得是一个空数组
/* User.find({ _id: '5c09f1e5aeb04b22f8460965' }).then(result => console.log(result)); */
//查询用户中年龄字段大于20小于40得文档
User.find({ age: { $gt: 20, $lt: 40 } }).then(result => console.log(result));
//查询兴趣爱好是足球得
User.find({ hobbies: { $in: ['足球'] } }).then(result => console.log(result));
//选择查询得字段 不想查询得字段 -_id
User.find().select('email name -_id').then(result => console.log(result));
//排序
User.find().sort('age').then(result => console.log(result));
//降序
User.find().sort('-age').then(result => console.log(result));
//skip limit限制 跳过前两个 查询三个
User.find().skip(2).limit(3).then(result);