Using Mongoskin to manipulate MongoDB

Source: Internet
Author: User
Tags install mongodb

Mongoskin is a model tool that operates MONGODB equivalent to the database class and Mongoose is quite famous.

Mounting module
CNPM Install MongoDB
CNPM Install Mongoskin

Import Mongoskin Connect MongoDB in the test database

var mongo = require (' Mongoskin '); var true});

Operation is much the same as MongoDB native syntax just a callback

Increase

Router.post ('/doadd ',function(req, res, next) {varparam =Req.body; if(Param.username &&Param.password) {        //Inserting DataDb.collection (' user '). Insert ({' username ': param.username, ' Password ':p Aram.password},function(err, result) {db.close (); if(!err) {Res.send ({' Status ': 1}); }Else{res.send ({' Status ': 0});    }        }); }});

By deleting

Router.post ('/dodel ',function(req, res, next) {varparam =Req.body; if(param.id) {varObjectId =MONGO.        ObjectID; Db.collection (' user '). Remove ({' _id ': ObjectId (param.id)},function(err, result) {db.close (); if(!err) {Res.send ({' Status ': 1}); }Else{res.send ({' Status ': 0});    }        }); }});

Change

Router.post ('/doedit ',function(req, res, next) {varparam =Req.body; if(Param.username &&Param.password) {        varObjectId =MONGO.        ObjectID; //Modify A single piece of dataDb.collection (' article '). Update ({' _id ': ObjectId (param._id), ' username ':p aram.username},{$set: {' Password ': Param.password}},function(err, result) {db.close (); if(!err) {Res.send ({' Status ': 1}); }Else{res.send ({' Status ': 0});    }        }); }});

Check

function (req, res, next) {    db.collection (' article '). Find (). ToArray (function  (err, result) {        Db.close ();        Res.render (' Admin/index ', {title: ' admin ', result:result}     );});

Using Mongoskin to manipulate MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.