1.mongoDB 安装自行百度
2.node express 自行百度
3.node 连接 mongodb 字符创 我这里是保存在json里面
"dbs": "mongodb://127.0.0.1:27017/soil" 如果带有用户名密码 "dbs": "mongodb://admin:admin@127.0.0.1:27017/soildev"
3.定义Schema
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var SoildataSchema = new Schema({
user: {
type:String
},
name: {
type:Number
}
})
module.exports = mongoose.model('Soildata', SoildataSchema)
//mongoose.model('Soildata', SoildataSchema) collection名称应该为第三个参数,若为缺省,会自动根据参数name的值以复数形式生成collection
4. server index.js 连接数据库
mongoose.connect(dbConfig.dbs, {
useNewUrlParser: true, useUnifiedTopology: true
})
5.查询:
var Soildata = require("./dbs/models/soildata.js")
Soildata.find({},function(err, doc){
if(err) {
console.log(err.message)
}else{
console.log(doc)
}
})
5.我的目录结构: