Javascript 猫鼬更新参考对象?
Javascript 猫鼬更新参考对象?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,对不起,我的英语不好。 我使用node.js+express.js+mongoose.js 我在mongoose中为组设置了此模式: var groupSchema = new mongoose.Schema({ name: String, users: [{type: mongoose.Schema.ObjectId, ref: 'User'}], posts: [{type: mongoose.Schema.ObjectId, ref: 'Post'}] }); 并
对不起,我的英语不好。
我使用node.js+express.js+mongoose.js 我在mongoose中为组设置了此模式:
var groupSchema = new mongoose.Schema({
name: String,
users: [{type: mongoose.Schema.ObjectId, ref: 'User'}],
posts: [{type: mongoose.Schema.ObjectId, ref: 'Post'}]
});
并将此模式发送给用户:
var userSchema = new mongoose.Schema({
login: { type: String, unique: true, lowercase: true },
password: String,
unread: [{type: mongoose.Schema.ObjectId, ref: 'Post'}]
});
组具有与此组相关的用户列表和与此组相关的帖子列表。我想实现的是:
Group1
有用户Mike
、John
和Jane
当用户
Mike
创建新帖子时:1)我找到当前组并选择与此组相关的用户(
Group1
和用户Mike
,John
和Jane
)2)对于用户
John
和Jane
我必须在未读
字段中设置创建的帖子。
(这样做是为了知道哪个post用户尚未阅读)。对吗?如果是,如何更新ref文档中的此未读字段?
我尝试过这样做:
示例:组的url:
http://localhost:3000/group/first
app.get('/group/:name', groups.getGroupPage);
app.post('/group/:name', posts.postCreate);
Posts.js
var Group = require('../models/group');
var User = require('../models/user');
var Post = require('../models/post');
exports.postCreate = function(req, res) {
var post = new Post({
title: req.body.p_title,
content: req.body.p_content,
author: req.user
});
Group
.update({ name: req.params.name }, {upsert:true}, { "$push": { "users.$.unread": post._id } })
.populate('users')
.exec(function(err,group) {
if (err) res.json(err)
console.log(group);
}
);
}
谢谢您的帮助。您的编码风格与我的略有不同,但我的做法如下:
exports.postCreate = function(req, res) {
//Create a new post
var newPost = new Post({
title: req.body.p_title,
content: req.body.p_content,
author: req.user
});
//Save the new post
newPost.save(function(err) {
if (err) {
res.json(err);
}
});
//Find the group with the name and populate the users field
Group.findOne({ name: req.params.name })
.populate('users')
.exec(function(err, group) {
if (err) {
res.json(err);
console.log(group);
}
if (group) {
//If group is found loop through the users
for (var i = o; i < group.users.length; i++) {
//Check if the current user is somebody else as the author
if (group.users[i]._id != req.user._id) {
//Push and save the new post to every user in the group
//We push the _id of the post because we are using references
group.users[i].unread.push(newPost._id);
group.users[i].save(function(err) {
if (err) {
throw err;
}
});
}
}
} else {
//Do what you need to do if group hasn't been found
}
});
};
exports.postreate=函数(请求、恢复){
//创建新帖子
var newPost=新职位({
标题:req.body.p_标题,
内容:req.body.p_内容,
作者:req.user
});
//保存新帖子
newPost.save(函数(err){
如果(错误){
res.json(err);
}
});
//查找具有名称的组并填充“用户”字段
Group.findOne({name:req.params.name})
.populate('用户')
.exec(函数(错误,组){
如果(错误){
res.json(err);
控制台日志(组);
}
国际单项体育联合会(小组){
//如果找到组,则通过用户循环
for(var i=o;i
此代码假定您的req.user
字段填充为用户模式
此代码未经测试。所以请调试它。:) 请张贴您自己尝试过的代码。我们需要更多的信息来帮助你。你在用快车吗?如果是,您使用的路线是什么?您将随请求向express发送什么信息?您是否随请求发送用户名或用户id?添加了更多代码,非常感谢,@Snekw。(我稍后会检查。)你觉得有没有更好的方法来检查用户是否查看了帖子?我认为我的方法不是最好的。我可以想出两种不同的方法。在用户和其他用户上存储关于谁查看过帖子的信息就是在帖子上存储信息。两者都有各自的优势。如果你不想轻易地了解每一个看过帖子的用户,那么把帖子存储在用户身上会更好。如果你想知道所有浏览过帖子的用户,那么我会将信息存储在帖子上,然后从那里检索。您可以将它们组合起来,并将信息存储在两者上,然后从那里开始工作。