mongodb 管理脚本

来源:互联网 发布:python 余弦距离 编辑:程序博客网 时间:2024/06/11 07:31

shell脚本:

这个脚本调用mongo客户端执行 js代码管理mongodb

#!/usr/bin/bashMONGOPATH=/home/zjw/mongo/mongodb-linux-x86_64-2.6.1/bin/mongo$MONGOPATH 127.0.0.1:27017/blog -eval "db.mycollection.remove({})"echo "data is clear"




-----------------------------------------------------------大笑-----我只是一条分隔线----大笑------------------------------------------------------------------------------


js脚本:

这个js脚本使用node 直接来管理mongo

clear.js

/** * Created by bloodchilde on 2015/7/7. */Db = require('mongodb').Db;Connection = require('mongodb').Connection;Server = require('mongodb').Server;var db="blog";var host="127.0.0.1";function gameData(){}gameData.prototype.removeAll = function(){    mongodb= new Db(db, new Server(host, 27017), {safe: true});    mongodb.open(function(err,db){        if(err){            return callback(err);        }        db.collection('myCollection',function(err,collection){            if(err){                mongodb.close();            }            collection.remove({},function(err,data){                if(err){                    mongodb.close();                }                console.log("the data is clear");                mongodb.close();            });        });    });};var demo  = new gameData();demo.removeAll();
init.sh

#!/usr/bin/bashnpm install mongodbecho "init success";


这两个脚本要合作才能使用,首先调用sh init.sh(或者./init.sh)来下载mongodb 模块,然后执行 node clear.js 来访问mongo



0 0
原创粉丝点击