是跟随尚硅谷的李立超老师学习的MongoDB 李立超老师MongoDB学习视频 下面把写过的代码文件放到上面,学习的小伙伴可以参考 01.helloMongoose.js
var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1/mongoose_test",);
mongoose.connection.once("open",function(){
console.log("数据库连接成功!!!");
});
mongoose.connection.once("close",function(){
console.log("数据库连接断开!!!");
});
mongoose.disconnect();
02.mongoose_demo.js
var mongoose =require("mongoose");
mongoose.connect("mongodb://127.0.0.1/mongoose_test");
mongoose.connection.once("open",function(){
console.log("数据库连接成功!!!")
});
var Schema = mongoose.Schema;
var stuSchema = new Schema({
name:String,
age:Number,
gender:{
type:String,
default:"female"
},
address:String
});
var StuModel = mongoose.model("student",stuSchema);
StuModel.create({
name:"白骨精",
age:16,
address:"白骨洞"
},function(err){
if(!err){
console.log("插入成功~~~");
}
});
03.model.js
var mongoose =require("mongoose");
mongoose.connect("mongodb://127.0.0.1/mongoose_test");
mongoose.connection.once("open",function(){
console.log("数据库连接成功!!!")
});
var Schema = mongoose.Schema;
var stuSchema = new Schema({
name:String,
age:Number,
gender:{
type:String,
default:"female"
},
address:String
});
var StuModel = mongoose.model("student",stuSchema);
04.document.js
var mongoose =require("mongoose");
mongoose.connect("mongodb://127.0.0.1/mongoose_test");
mongoose.connection.once("open",function(){
console.log("数据库连接成功!!!")
});
var Schema = mongoose.Schema;
var stuSchema = new Schema({
name:String,
age:Number,
gender:{
type:String,
default:"female"
},
address:String
});
var StuModel = mongoose.model("student",stuSchema);
var stu = new StuModel({
name:"奔波霸",
age:48,
gender:"male",
address:"碧波潭"
});
StuModel.findOne({},function (err,doc) {
if (!err) {
doc = doc.toObject();
delete doc.address;
console.log(doc._id);
}
});
05.index.js
require("./tools/conn_mongo");
var student = require("./models/student").model;
student.find({},function(err,docs){
if(!err){
console.log(docs);
}
});
student.js
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var stuSchema = new Schema({
name:String,
age:Number,
gender:{
type:String,
default:"female"
},
address:String
});
var stuModel = mongoose.model("student",stuSchema);
exports.model = stuModel;
|