python | mongoose based on the additions and deletions to change search operation

Whether it is based on robomongo visualization tools, it is also based on a function or tool mongoose, as long as the operation of mongodb, the first step is to open the database. 

Open Database mongodb 
enter mongod directory Run ./mongod --dbpath = storage of data 
Example 1: ./ mongod --dbpath d: \ MongoDB \ db 
Example 2: ./ mongod --dbpath d: \ MongoDB \ db --port custom port number 27017 by default (you can understand, is not recommended to modify the default port number for post-maintenance troubles) 
new data 
the let Mongoose = The require ( 'Mongoose'); 
the let console.log.bind = log (Console ); 
the let DB = null; 
the let the Schema = mongoose.Schema; 

// link database 
mongoose.connect ( 'MongoDB: // localhost / Fay'); 

DB = mongoose.connection; 

db.on ( 'error', console.error .bind (Console, 'Connection error:')) 

db.once ( 'Open', (CB) => { 
    // set the type of database 
    the let PersonSchema the Schema new new = ({ 
        name: {
            type: 'String', 
            required: to true 
        }, 
        Age: { 
            type: 'Number The', 
            retuired: to true 
        } 
    }); 

    // create a new set (e.g., the presence of the set, it is checked) 
    the let PersonModel db.Model = ( 'Person' , PersonSchema); 

    // save data required 
    the let = {data 
        Age: 50, 
        name: 'Leno' 
    }; 

    // instantiate data set and insert 
    the let personEntity = new new PersonModel (data); 

    // save the instance 
    personEntity.save ( (ERR, RES) => { 
        IF (ERR) return log (ERR); 
        db.Close (); 
    }); 
}) 
delete data 

let mongoose = require ( 'mongoose');
= log console.log.bind the let (Console);  
the let DB = null;
the let the Schema = mongoose.Schema; 

// link database 
mongoose.connect ( 'MongoDB: // localhost / Fay'); 

DB = mongoose.connection; 

db.on ( 'error ', console.error.bind (Console,' Connection error: ')) 

db.once (' Open ', (CB) => { 
    // set the type of database 
    the let PersonSchema the Schema new new = ({ 
        name: { 
            type:' String ', 
            required: to true 
        }, 
        Age: { 
            type:' Number the ', 
            retuired: to true 
        } 
    }); 

    // set selection (e.g., the set does not exist, new) 
    the let PersonModel db.Model = (' Person ', PersonSchema) ; 

    // delete conditions 
    let del = {name: 'leno'};

    // delete command
    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: to true 
        }, 
        Age: { 
            type: 'Number The', 
            retuired: to true 
        } 
    }); 
}) 
query data
    }); 

    // set selection (e.g., the set does not exist, new) 
    the let PersonModel db.Model = ( 'Person', PersonSchema); 

    // old data 
    the let oldVal = {name:; 'Leno'} 

    // new data 
    name = {newVal the let: 'Liao'}; 
    // new data a plurality of 
    the let newVal2 = {name: 'Liao', Age: '25'}; 

    // modified (updated) command 
    PersonModel.update (oldVal, newVal, ( ERR, RES) => { 
        IF (ERR) the throw new new Error (ERR); 

        log (RES); 
        db.Close ();
Mongoose = the require the let ( 'Mongoose'); 
the let console.log.bind = log (Console); 
the let DB = null; 
the let the Schema = mongoose.Schema; 

// link database 
mongoose.connect ( 'mongodb: // localhost / fay '); 

DB = mongoose.connection; 
    }); 
    // set selection (e.g., the set does not exist, New)

db.on ( 'error', console.error.bind (Console, 'Connection error:')) 

db.once ( 'Open', (CB) => { 
    // set the type of database 
    the let PersonSchema the Schema new new = ({ 
        name : { 
            type: 'String', 
            required: to true 
        }, 
        Age: { 
            type: 'Number The', 
            retuired: to true 
        } 

    the let PersonModel db.Model = ( 'Person', PersonSchema); 

    // query 
    let sql = {name: 'Liao'}; 

    // query command 
    PersonModel.find (SQL, (ERR, RES) => { 
        IF (ERR) the throw new new Error (ERR); 

        log (RES); 
        db.Close (); 
    }); 
}) ;
Additional field
In the existing collection, add new fields. schema.add ()

  

Guess you like

Origin www.cnblogs.com/huangjiangyong/p/12131471.html