模式定义:
//行程
var CarpoolTralvelSchema = new Schema({
car:{
type:Schema.Types.ObjectId,
ref:"carpoolCar"
},//车辆信息
travel:{
type:Schema.Types.ObjectId,
ref:"carpoolTralvel"
},//搭车人引用的开车人发起的CarpoolTralvel id
userId:String,//用户id
userName:String,//用户名字
userType:Number,//1:开车人;2:搭车人
startCityCode:String,//出发城市代码
startCity:String,//出发城市
startAddress:String,//出发地点
arriveCityCode:String,//达到城市代码
arriveCity:String,//达到城市
arriveAddress:String,//到达地点
startDate:Number,//出发日期时间
travelSerialNumber:String,//行程流水号(8位数字,递增)
twoDimensionalCode:String,//二维码(行程流水号+开车人id+搭车人id)整体base64
seatCount:Number,//提供座位数
bookedSeatCount:Number,//已预定座位数
seatState:Number,//0 未满;1已满;
filter:[],//要求
state:Number,//0:正常 1:被踢 2:取消行程 3取消预约
// evaluates:[],//评价记录
// rejects:[],//拒绝记录
createdAt:Number,//建立日期时间
updateTime:Number,//最后修改日期时间
createUser:String,//创建人
remark:String //备注
});
//汽车
var CarpoolCarSchema = new Schema({
carOwnerId:String,//车主id(工号)
carOwnerName:String,//车主姓名
carType:String,//车型 suv 房车
carSeat:Number,//座位数
carModel:String,//品牌 bmw
carColor:String,//颜色id
carNumber:String,//车牌
carImg:{
type:Schema.Types.ObjectId,
ref:"carpoolAttachment"
},//图片
createdAt:Number,//建立日期时间
updateTime:Number//最后修改日期时间
});
常用crud
function findOne(Response) {
var db = mongoose.createConnection("mongodb://10.10.1.147:27017/mobile");
var carModel = db.model("carpoolCar", sm.CarpoolCarSchema);
carModel.findOne({
userId : "xxx"
}).exec(function(err, doc) {
db.close();
console.log("doc", doc);
Response.end("haha");
});
}
function findById(Response) {
var db = mongoose.createConnection("mongodb://mobile:[email protected]:27017/mobile");
var carModel = db.model("carpoolCar", sm.CarpoolCarSchema);
carModel.findById("xxx", function(err, doc) {
db.close();
console.log("doc", doc);
Response.end("haha");
});
}
function findOneAndUpdate(Response) {
var db = mongoose.createConnection("mongodb://mobile:[email protected]:27017/mobile");
var pushMessageModel = db.model("basePushMessage", sm.BasePushMessageSchema);
pushMessageModel.findOneAndUpdate({
appId : "333",
userId : "888"
}, {
$inc : {
badgeNum : 1
}
}, {
upsert : true
}, function(err, doc) {
db.close();
console.log(err);
Response.end(JSON.stringify(doc));
});
}
function insertTest(Response) {
var db = mongoose.createConnection("mongodb://10.10.1.147:27017/mobile");
var carpoolTravelCountModel = db.model("carpoolTravelCount", sm.CarpoolTravelCountSchema);
var carpoolTravelCountEntity = new carpoolTravelCountModel({
userId : "0658",
count1 : 333,
count2 : 888
});
carpoolTravelCountEntity.save(function(err, doc) {
db.close();
if (!err) {
Response.end(JSON.stringify({
data : doc
}));
} else {
Response.end("fail");
}
});
}
function findById2(Response) {
var db = mongoose.createConnection("mongodb://mobile:[email protected]:27017/mobile");
var attachmentModel = db.model("carpoolAttachment", sm.CarpoolAttachmentSchema);
var carModel = db.model("carpoolCar", sm.CarpoolCarSchema);
carModel.findById("54b3ceeca89ea0321625b1e5").populate("carImg").exec(function(err, data) {
Response.setHeader("Content-type", "text/json;charset=utf-8");
db.close();
if (!err) {
Response.end(JSON.stringify({
data : data
}));
} else {
console.log("err--->", err);
Response.end("fail");
}
});
}
function subDocumentTest(Response) {
var db = mongoose.createConnection("mongodb://mobile:[email protected]:27017/mobile");
var testModel = db.model("test", sm.TestSchema);
var testEntity = new testModel({
name : "hello",
address : {
city : "北京",
street : "朝阳"
}
});
testEntity.save(function(err, doc) {
Response.setHeader("Content-type", "text/json;charset=utf-8");
db.close();
console.log("err", err);
if (!err) {
Response.end(JSON.stringify({
status : "0",
doc : doc
}));
} else {
Response.end("fail");
}
});
}
function huiyishiTest(Param, Response) {
var startTime = parseInt(Param.params.startTime);
var endTime = parseInt(Param.params.endTime);
var db = mongoose.createConnection("mongodb://wdh:[email protected]:27017/wdh");
var HuiyishiSchema = new Schema({
name : String,
admin : [],
startTime : Number,
endTime : Number
});
var huiyishiModel = db.model("huiyishi", HuiyishiSchema);
Response.setHeader("Content-type", "text/json;charset=utf-8");
huiyishiModel.find({
startTime : {
"$lte" : endTime
},
endTime : {
"$gte" : startTime
}
}, {
_id : 1
}).exec(function(err, data) {
if (!err) {
console.log("被预定会议室:", data);
//查询出没有被预约的会议室
huiyishiModel.find({
_id : {
"$nin" : data
}
}).exec(function(err, data1) {
db.close();
if (!err) {
Response.end(JSON.stringify({
status : "0",
msg:data1.length>0?"有空闲会议室":"没有空闲会议室",
data : data1
}));
} else {
console.log(err);
Response.end(JSON.stringify({
status : "-1",
msg : "查询失败"
}));
}
});
;
} else {
db.close();
Response.end("查询失败");
}
});
}
function dynamicQuery(Response){
var arg = {name:"root1"};
var query = {};
if(arg.root1){
query.name = arg.name;
}
if(arg.startTime){
query.startTime = parseInt(arg.startTime);
}
console.log(query);
var db = mongoose.createConnection("mongodb://wdh:[email protected]:27017/wdh");
var HuiyishiSchema = new Schema({
name : String,
admin : [],
startTime : Number,
endTime : Number
});
var huiyishiModel = db.model("huiyishi", HuiyishiSchema);
huiyishiModel.find(query).exec(function(err,data){
db.close();
Response.setHeader("Content-type", "text/json;charset=utf-8");
if(!err){
Response.end(JSON.stringify({
status:"0",
data:data
}));
}else{
Response.end("fail");
}
});
}