Node使用MongoDB

const mongoose = require('mongoose'); 

// 建立连接
mongoose.connect('mongodb://192.168.1.188:27017/playground')
    .then(() => console.log('mongodb连接成功! '))
    .catch((err) => console.log(err, 'mongodb连接失败!'))
;

// 创建集合规则
const bookSchema = new mongoose.Schema({
    name: String,
    author: String,
    isPublished: Boolean,
    hobbies: [String],
    age: Number,
    money: {
        type: String,
        required: [true, '请传入金额'],
        minlength: 2,
        maxlength: 10,
        trim: true
    }
});
// 创建集合
const Book = mongoose.model('Book', bookSchema);

// 插入文档 方式1
const book1 = new Book({
    name: 'node.js',
    author: 'Peter',
    isPublished: true
});
book1.save();

// 插入文档 方式2
Book.create({name: 'Java学习',author: 'Peter',isPublished: true }, (err,doc)=>{
    console.log(err);
    console.log(doc);
});

Book.create({name: 'Python学习',author: 'Peter',isPublished: true })
        .then(doc => console.log(doc))
        .catch(err => console.log(err))
;

/*Book.create({name: 'Python学习',author: 'Peter',isPublished: true, age: 10, hobbies:['唱歌','跳舞'] })
        .then(doc => console.log(doc))
        .catch(err => console.log(err))
;

Book.create({name: 'Big学习',author: 'Peter',isPublished: true, age: 20, hobbies:['唱歌','跳舞'] })
        .then(doc => console.log(doc))
        .catch(err => console.log(err))
;

Book.create({name: 'Pen学习',author: 'Peter',isPublished: true, age: 30, hobbies:['唱歌','跳舞'] })
        .then(doc => console.log(doc))
        .catch(err => console.log(err))
;*/

// 查询文档
Book.find().then( result => console.log(result) ); //返回数组
Book.find({_id: '6491450999339e6089c52876'}).then( result => console.log(result) ); //返回数组
Book.findOne({_id: '6491450999339e6089c52877'}).then( result => console.log(result) );//返回1个对象
// 大于小于
Book.find({age: {$gt: 10, $lt: 30}}).then(result => console.log(result));
// 包含
Book.find({hobbies: {$in: ['唱歌']}}).then(result => console.log(result));
// 指定要返回的字段 -代表不返回的字段
Book.find().select('name age -_id').then(result => console.log(result));
// 按age升序
Book.find().select('name age -_id').sort('age').then(result => console.log(result));
// 按age降序
Book.find().select('name age -_id').sort('-age').then(result => console.log(result));
// 分页 skip跳过多少行 limit 显示多少行
Book.find().skip(2).limit(2).then(result => console.log(result));

// 删除文档
// 查询出满足条件的第一条并删除
Book.findOneAndDelete({_id: '64914799048fb98b2f5e445e'}).then(result => console.log(result));
// 删除多条
// 危险: 参数是{} 时,会全部删除  { acknowledged: true, deletedCount: 4 }
Book.deleteMany({}).then(result => console.log(result)); 
 
// 更新文档 
// 查询条件 {_id: '64914799048fb98b2f5e445e'}
// 要修改的值 {name: 'Node学习'}
Book.updateOne({_id: '64914af681f7294f5c12e8b3'}, {name: 'Node学习'}).then(result => console.log(result)); 
Book.updateMany({}, {age: 100}).then(result => console.log(result)); 


//MongoDB的集合关联
const User = mongoose.model('User', new mongoose.Schema({name: {type: String, required: true} }));

const Post = mongoose.model('Post', new mongoose.Schema({
    title: {type:String},
    author: { type: mongoose.Schema.Types.ObjectId, ref: 'User'} //关联User
}));

// User.create({name: '张三'}).then(result => console.log(result));
// Post.create({title: '123', author: '6491558e3beb5dde02186484'}).then(result => console.log(result));

//联合查询
Post.find().populate('author').then(result => console.log(result));

 

posted @ 2023-06-20 15:34  Peter.Jones  阅读(11)  评论(0编辑  收藏  举报