zoukankan      html  css  js  c++  java
  • python | 基于mongoose 的增删改查操作

    无论是基于robomongo 的可视化工具,亦或是基于 mongoose 的函数工具,只要是对 mongodb 的操作,第一步都是开启数据库。
    
    开启mongodb 数据库
    进入mongod所在目录 执行命令 ./mongod --dbpath=存放数据的位置
    例1:./mongod --dbpath d:MongoDBdb
    例2:./mongod --dbpath d:MongoDBdb --port 自定义端口号,默认27017(了解即可,不推荐使用,修改默认端口号后期维护麻烦)
    新增数据
    let mongoose = require('mongoose');
    let log = console.log.bind(console);
    let db = null;
    let Schema = mongoose.Schema;
    
    // 链接数据库
    mongoose.connect('mongodb://localhost/fay');
    
    db = mongoose.connection;
    
    db.on('error', console.error.bind(console, 'connection error:'))
    
    db.once('open', (cb) => {
        // 设置数据库类型
        let PersonSchema = new Schema({
            name: {
                type: 'String',
                required: true
            },
            age: {
                type: 'Number',
                retuired: true
            }
        });
    
        // 新建集合(如存在该集合,就选中)
        let PersonModel = db.model('person', PersonSchema);
    
        // 需要保存的数据
        let data =  {
            age: 50,
            name: 'leno'
        };
    
        // 实例化集合并插入数据
        let personEntity = new PersonModel(data);
    
        //  保存实例
        personEntity.save((err, res) => {
            if(err) return log(err);
            db.close();
        });
    })
    删除数据
    
    let mongoose = require('mongoose');
    let log = console.log.bind(console);
    let db = null;
    let Schema = mongoose.Schema;
    
    // 链接数据库
    mongoose.connect('mongodb://localhost/fay');
    
    db = mongoose.connection;
    
    db.on('error', console.error.bind(console, 'connection error:'))
    
    db.once('open', (cb) => {
        // 设置数据库类型
        let PersonSchema = new Schema({
            name: {
                type: 'String',
                required: true
            },
            age: {
                type: 'Number',
                retuired: true
            }
        });
    
        // 选择集合(如不存在该集合,就新建)
        let PersonModel = db.model('person', PersonSchema);
    
        // 删除的条件
        let del = {name: 'leno'};
    
        // 删除命令
        PersonModel.remove(del, (err, res) => {
            if(err) throw new Error(err);
    
            log(res);
            db.close();
        })
        
    });
    修改数据
    let mongoose = require('mongoose');
    let log = console.log.bind(console);
    let db = null;
    let Schema = mongoose.Schema;
    
    // 链接数据库
    mongoose.connect('mongodb://localhost/fay');
    
    db = mongoose.connection;
    
    db.on('error', console.error.bind(console, 'connection error:'))
    
    db.once('open', (cb) => {
        // 设置数据库类型
        let PersonSchema = new Schema({
            name: {
                type: 'String',
                required: true
            },
            age: {
                type: 'Number',
                retuired: true
            }
        });
    
        // 选择集合(如不存在该集合,就新建)
        let PersonModel = db.model('person', PersonSchema);
    
        // 旧数据
        let oldVal = {name: 'leno'};
    
        // 新数据
        let newVal = {name: 'liao'};
        // 多个新数据
        let newVal2 = {name: 'liao', age: '25'};
    
        // 修改(更新)命令
        PersonModel.update(oldVal, newVal, (err, res) => {
            if(err) throw new Error(err);
    
            log(res);
            db.close();
        });
    })
    查询数据
    let mongoose = require('mongoose');
    let log = console.log.bind(console);
    let db = null;
    let Schema = mongoose.Schema;
    
    // 链接数据库
    mongoose.connect('mongodb://localhost/fay');
    
    db = mongoose.connection;
    
    db.on('error', console.error.bind(console, 'connection error:'))
    
    db.once('open', (cb) => {
        // 设置数据库类型
        let PersonSchema = new Schema({
            name: {
                type: 'String',
                required: true
            },
            age: {
                type: 'Number',
                retuired: true
            }
        });
    
        // 选择集合(如不存在该集合,就新建)
        let PersonModel = db.model('person', PersonSchema);
    
        // 查询条件
        let sql = {name: 'liao'};
    
        // 查询命令
        PersonModel.find(sql, (err, res) => {
            if(err) throw new Error(err);
    
            log(res);
            db.close();
        });
    });
    追加字段
    在已存在的集合中,添加新的字段。schema.add()
    

      

  • 相关阅读:
    基于FFI模块CAPI与JavaScript的各种类型匹配总结
    在Electron中通过ffi模块实现JavaScript调用C++动态库
    谷歌地图OGC WMTS服务规则
    tiff/tfw, jpg/jpgw坐标文件的格式(6个参数)
    GreenDao 多表事务操作
    Asp.net WebAPI 使用流下载文件注意事项
    mvn 用指定setting.xml 执行指定pom.xml
    Swagger自动生成接口文档
    Windows下控制Nginx的状态
    Android 动态权限申请
  • 原文地址:https://www.cnblogs.com/huangjiangyong/p/12131471.html
Copyright © 2011-2022 走看看