[英]Referencing between two collections not working on MongoDB
I am creating a user based node app and implementing the DHTMLX Scheduler within it. 我正在创建一个基于用户的节点应用程序,并在其中实现DHTMLX Scheduler。 I have the scheduler working and showing events, the only problem is every user sees and edits the same calendar as of right now.
我让调度程序工作并显示事件,唯一的问题是每个用户现在都可以看到和编辑相同的日历。
I tried creating schemas with references but it hasn't seemed to work. 我尝试使用引用创建架构,但是似乎没有用。
player.js model (each individual user Schema): player.js模型(每个用户模式):
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
calendar: {
type: Schema.Types.ObjectId, ref: 'calendar'
}
});
const User = mongoose.model('player', UserSchema);
module.exports = User;
calendar.js model: calendar.js模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schema = new Schema({
text: {type: String, required = true},
start_date: {type: Date, required = true},
end_date: {type: Date, required = true},
user: {type: Schema.Types.ObjectId, ref = 'User', required = true}
});
const calendar = mongoose.model('calendar', schema);
module.exports = calendar;
Implementing the calendar portion of my app.js 实现我的app.js的日历部分
var db = require('mongoskin').db("myMongoDBCluster", { w: 0});
db.bind('calendar');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/init', function(req, res){
db.calendar.insert({
text:"My test event A",
start_date: new Date(2018,8,1),
end_date: new Date(2018,8,5)
});
db.calendar.insert({
text:"My test event B",
start_date: new Date(2018,8,19),
end_date: new Date(2018,8,24)
});
db.calendar.insert({
text:"Morning event",
start_date: new Date(2018,8,4,4,0),
end_date: new Date(2018,8,4,14,0)
});
db.calendar.insert({
text:"One more test event",
start_date: new Date(2018,8,3),
end_date: new Date(2018,8,8),
color: "#DD8616"
});
res.send("Test events were added to the database")
});
app.get('/data', function(req, res){
db.calendar.find().toArray(function(err, data){
//set id property for all records
console.log(err);
for (var i = 0; i < data.length; i++)
data[i].id = data[i]._id;
//output response
res.send(data);
});
});
app.post('/data', function(req, res){
var data = req.body;
var mode = data["!nativeeditor_status"];
var sid = data.id;
var tid = sid;
delete data.id;
delete data.gr_id;
delete data["!nativeeditor_status"];
function update_response(err, result){
if (err)
mode = "error";
else if (mode == "inserted")
tid = data._id;
res.setHeader("Content-Type","application/json");
res.send({action: mode, sid: sid, tid: tid});
}
if (mode == "updated")
db.calendar.updateById( sid, data, update_response);
else if (mode == "inserted")
db.calendar.insert(data, update_response);
else if (mode == "deleted")
db.calendar.removeById( sid, update_response);
else
res.send("Not supported operation");
});
Mongo is a non-relational database, "reference" is a feature provided by mongoose. Mongo是非关系数据库,“引用”是mongoose提供的功能。 To make use of it you have to query through mongoose Models (objects exported from player.js and calendar.js)
要使用它,您必须查询猫鼬模型(从player.js和calendar.js导出的对象)
var Calendar = require('./calendar');
Calendar.find()
.then(function (data) {
// ...
});
Calendar.find({user: 'yourUserId'}) // query by specific user
.then(function (data) {
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.