猫鼬关系不能双向工作
Mongoose relation not working both ways
我无法在我的应用程序(使用 yeoman angular-fullstack 生成器构建)中的 Rides 和 Comments 控制器之间建立关系 运行。
评论模型:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CommentSchema = new Schema({
name: String,
comment: String,
active: Boolean,
ride: { type: mongoose.Schema.Types.ObjectId, ref: 'Ride' }
});
module.exports = mongoose.model('Comment', CommentSchema);
乘坐车型:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var RideSchema = new Schema({
name: String,
distance: String,
climb: String,
rating: String,
active: Boolean,
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
module.exports = mongoose.model('Ride', RideSchema);
访问 /api/comments/ 给我一个正确的结果,其中包含相关的行程:
{"_id":"54ce818f8c2889da58b01e19","name":"NAAM","comment":"COMMENT","ride":"54ce69647a78532057aa98e0","__v":0}]
访问 /api/rides/ 得到以下结果,没有相应的评论:
[{"_id":"54ce69647a78532057aa98e0","name":"Ride test ingevuld","distance":"4000","climb":"1200","rating":"1","__v":0,"comments":[]}]
谁能告诉我我做错了什么?
据我所知,它不是那样工作的。你的评论得到了它的旅程,你的旅程得到了它的评论。我认为,您应该删除
ride: { type: mongoose.Schema.Types.ObjectId, ref: 'Ride' }
并在乘车集合中发表评论。
comments: ['Comment']
它更多的是 objective 解决方案,因为它应该在为 objective(分层)数据设计的 MONGO 数据库中。
来自我的一个项目的示例:
exports.insertRoom = function(req, res) {
var id = req.body.id;
var r = req.body.room;
var room = new Room({name: r.name});
Floor.update(
{_id : id},
{
$push: { rooms: room}
},
{upsert:true},
function(floor, err)
{
res.sendStatus(200);
}
);
};
我无法在我的应用程序(使用 yeoman angular-fullstack 生成器构建)中的 Rides 和 Comments 控制器之间建立关系 运行。
评论模型:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CommentSchema = new Schema({
name: String,
comment: String,
active: Boolean,
ride: { type: mongoose.Schema.Types.ObjectId, ref: 'Ride' }
});
module.exports = mongoose.model('Comment', CommentSchema);
乘坐车型:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var RideSchema = new Schema({
name: String,
distance: String,
climb: String,
rating: String,
active: Boolean,
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
module.exports = mongoose.model('Ride', RideSchema);
访问 /api/comments/ 给我一个正确的结果,其中包含相关的行程:
{"_id":"54ce818f8c2889da58b01e19","name":"NAAM","comment":"COMMENT","ride":"54ce69647a78532057aa98e0","__v":0}]
访问 /api/rides/ 得到以下结果,没有相应的评论:
[{"_id":"54ce69647a78532057aa98e0","name":"Ride test ingevuld","distance":"4000","climb":"1200","rating":"1","__v":0,"comments":[]}]
谁能告诉我我做错了什么?
据我所知,它不是那样工作的。你的评论得到了它的旅程,你的旅程得到了它的评论。我认为,您应该删除
ride: { type: mongoose.Schema.Types.ObjectId, ref: 'Ride' }
并在乘车集合中发表评论。
comments: ['Comment']
它更多的是 objective 解决方案,因为它应该在为 objective(分层)数据设计的 MONGO 数据库中。
来自我的一个项目的示例:
exports.insertRoom = function(req, res) {
var id = req.body.id;
var r = req.body.room;
var room = new Room({name: r.name});
Floor.update(
{_id : id},
{
$push: { rooms: room}
},
{upsert:true},
function(floor, err)
{
res.sendStatus(200);
}
);
};