美文网首页
nodejs操作数据库mongodb

nodejs操作数据库mongodb

作者: 天字一等 | 来源:发表于2018-10-10 18:59 被阅读15次

全部代码:
html代码:

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>viewOne</title>
   <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
   <meta name="apple-mobile-web-app-capable" content="yes">
   <meta name="apple-mobile-web-app-status-bar-style" content="black">
   <meta name="format-detection" content="telephone=no" />
   <link rel="stylesheet" type="text/css" href="../public/style.css" />
</head>
<body>
<div class="content">
   <header class="common-header">
       <a class="icon-back" href="javascript:history.go(-1);"></a>
       <span>viewTwo取消</span>
   </header>
   <div>
       <form action="http://192.168.10.57:4000/regist" method="get">
           <input type="text" name="first_name" />
           <br>
           <input type="text" name="last_name"/>
           <input type="submit" value="提交">
       </form>
   </div>
</div>
</body>
<script type="text/javascript">
   var adaptPageClass = 'content';
</script>
</html>

后台代码:

var MongoClient = require('mongoose');
MongoClient.connect('mongodb://localhost:27017/node数据库',{ useNewUrlParser: true },function () {  //27017是数据库端口,node数据库是数据库名
    console.log('test')
});
//提示数据库是否链接成功
var db = MongoClient.connection;
db.on('open',function () {
    console.log('MongoDB链接成功')
})
db.on('error',function () {
    console.log('MongoDB连接失败');
})
var Schema = MongoClient.Schema; //创建一个Schema模型骨架,并且设置好user模版
var userSchema = new Schema({
    firstName:{type:String},
    lastName:{type:String}
});
var User = MongoClient.model("User",userSchema);  //Schema发布一个User的Model
app.get('/regist',function (req,res) {  处理regist的get请求
    console.log('轻取i成功')
    console.log(req.query.first_name)
    var user_1 = new User({
        firstName: req.query.first_name,
        lastName: req.query.last_name
    });
    user_1.save(function () {
        res.send('数据插入成功');  //插入数据并且提示数据库插入成功
    });
});

1、链接数据库:

var MongoClient = require('mongoose');
MongoClient.connect('mongodb://localhost:27017/node数据库',{ useNewUrlParser: true },function () {  //27017是数据库端口,node数据库是数据库名
    console.log('test')
});

//提示数据库是否链接成功
var db = MongoClient.connection;
db.on('open',function () {
    console.log('MongoDB链接成功')
})
db.on('error',function () {
    console.log('MongoDB连接失败');
})

2、创建一个Schema骨架模型,并且设计好user模板

var Schema = MongoClient.Schema; //创建一个Schema模型骨架,并且设置好user模版
var userSchema = new Schema({
    firstName:{type:String},
    lastName:{type:String}
});

3、发布模型

var User = MongoClient.model("User",userSchema);  //Schema发布一个User的Model

4、处理前端请求

app.get('/regist',function (req,res) {  处理regist的get请求
    console.log('轻取i成功')
    console.log(req.query.first_name)
    var user_1 = new User({
        firstName: req.query.first_name,
        lastName: req.query.last_name
    });
    user_1.save(function () {
        res.send('数据插入成功');  //插入数据并且提示数据库插入成功
    });
});

借鉴自:https://blog.csdn.net/qwe502763576/article/details/79646551
http://vince.xin/article/5acc33286b78214ab8ac58b6

相关文章

  • XDH_LESSON8

    nodejs操作mongodb数据库 NodeJs为什么要用mongoose操作mongodb直接用mongodb...

  • 学习笔记-0815-nodejs 操作mongodb数据库

    nodejs 操作mongodb数据库 黄金搭档nodejs 自带组件mongodb/mongoose mongo...

  • 0815_mongodb的CURD

    新内容(mongodb-crud操作) nodejs操作mongodb数据库 npm install mangod...

  • mongodb学习3

    Nodejs 操作 MongoDb数据库 在 Nodejs 中使用 MongodbNodejs 连接 MongoD...

  • 0816

    node.js 操作mongodb数据库 nodejs 自带组件 mongodb/mongoose npm ins...

  • 20160815 nodejs对mongodb的操作

    nodejs 操作 mongodb 数据库 新建文件夹 npm install mongodb npm insta...

  • 2016年8月15日学习笔记

    nodejs操作mongodb数据库 知识点 安装数据库出现错误:--journal --storageEnain...

  • nodejs使用mongodb将string类型id转换为Obj

    一、问题描述 通过NodeJs操作MongoDb数据库,在删除操作中使用主键id做为条件: 有接触过mongodb...

  • Number two

    NodeJs 操作数据库详解 1. 引入mongodb模块 var mongoclient = require("...

  • zhouyi

    nodejs 操作数据库 这是黄金搭档 mongodb 有很多组建 nodejs 有专门的驱动 save 加了 -...

网友评论

      本文标题:nodejs操作数据库mongodb

      本文链接:https://www.haomeiwen.com/subject/blsmaftx.html