Mongodb+nodejs additions and deletions of the demo

Source: Internet
Author: User
Tags findone mongodb

1. Start the database

Display after startup is complete

The port number is 27017

2. Create a database to create a database named MyDB

3. First check the users of course, and then add a

4. Create a data table and query all the tables

Db.createcollection ("account")

  

5. New data

  Db.account.insert ({_id:10, type: "Misc", Item: "Card", qty:15})

Where account is the name of the table

  

6. Then we can combine Nodejs+mongodb.

 

This is the structure of this project.

This is the Setting.js file

This is the Result.js file

Base.js file

User.js file

User.js is a model document that does not process data

7. Finding the method in your own project is not the hardest, and it is difficult for me to find my own path.

I added it to the App.js project.

App.use ('/login ', require ('./routes/login '));
This jumps to the login page
App.use ('/users ', require ('./routes/users ')); This is the user information processing of the total JS

It's ugly, but it can be done with basic needs.

This is the code for the page

<!DOCTYPE HTML><HTML><Head>    <title><%=title%></title>    <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/></Head><Body><H1>Login</H1><P>Warmly welcome</P><formname= "Login"Method= "POST"Action= "/users/loginaction"><Div>Phone Number:<inputtype= "text"name= "mobile"/></Div><Div>Password:<inputtype= "text"name= "Password"/></Div>    <Buttontype= "Submit"value= "Login">Login</Button>    <inputtype= "Submit"formaction= "/users/register"value= "Register" /></form></Body></HTML>

8. Processing business logic to increase users, delete users, query user list (./routes/users)

varExpress = require (' Express ');varRouter =Express. Router (); Console.log ("User.js")varUserentity = require ('.. /models/user '). userentity;varRestresult = require ('.. /result '); Router.post ('/register ',function(Req,res,next) {Console.log ("Zhuce")  varRestresult =NewRestresult (); varMobile =Req.body.mobile; varPassword =Req.body.password; if(!password | | Password < 6) {Restresult.errorcode=Restresult.illegal_argument_error_code; Restresult.errorreason= "Password length cannot be less than 6 pull";    Res.send (Restresult); return; }  //FindOne method, the first parameter number condition, the second parameter is the field projection, the third parameter is the callback functionUserentity.findone ({mobile:mobile}, ' _id ',function(err,user) {if(Err) {}if(user) {Restresult.errorcode=Restresult.business_error_code; Restresult.errorreason= "Mobile number registered";      Res.send (Restresult); return; }    varRegisterUser =Newuserentity ({mobile:mobile,password:password}); Registeruser.save (function(err,row) {if(Err) {Restresult.errorcode=Restresult.server_exception_error_code; Restresult.errorreason= "Server has a problem";        Res.send (Restresult); return; } console.log ("Insert Succeeded");    Res.send (Restresult); })}) Router.get (‘/‘,function(req, res, next) {Res.render (' Login ', {title: ' Express ' });}); Router.post ('/remove ',function(req,res,next) {varID =req.body.id; Console.log (' ID: ' +ID); Userentity.remove ({_id:id},function() {Console.log (' Delete succeeded ') Res.send ({code:1}); })})//Landing RouteRouter.post ('/loginaction ',function(Req,res,next) {Console.log ("Sign In"); varRestresult =NewRestresult (); varMobile =Req.body.mobile; Console.log ("Mobile:" +mobile) Console.log ("Password:" +mobile)if(!/1\d{10}/.test (MOBILE)) {//mobile phone number format CheckRestresult.errorcode =Restresult.illegal_argument_error_code; Restresult.errorreason= "Please fill in the True phone format";    Res.send (Restresult); return; }  varPassword =Req.body.password; if(!password) {Restresult.errorcode=Restresult.illegal_argument_error_code; Restresult.errorreason= "Password cannot be empty";    Res.send (Restresult); return; } userentity.findone ({mobile:mobile,password:password},{password:0},function(err,user) {if(Err) {Restresult.errorcode=Restresult.server_exception_error_code; Restresult.errorreason= "Server Exception";      Res.send (Restresult); return; }    if(!user) {Restresult.errorcode=Restresult.business_error_code; Restresult.errorreason= "User name or password error";      Res.send (Restresult); return; }  /*restresult.returnvalue = user; Res.send (Restresult);*/userentity.find ({},{},function(E,docs) {Res.render (' UserList ',{        "UserList":d OCS});    }); //Update last Login timeUserentity.update ({_id:user._id},{$set: {lastlogintime:NewDate ()}}).  exec (); });}); Module.exports= Router;

9, last look at the effect

Mongodb+nodejs additions and deletions of the demo

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.