對不起,我的英語不好。
我用的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
我必須在unread
字段中設置創建的帖子。 (讓這個知道,哪個用戶還沒有閱讀)。
它是正確的嗎?如果是的話,我如何更新參考文件中的這個未讀字段?
我試着這樣做:
在例如:組網址: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);
}
);
}
感謝的任何幫助。
請發表您自己試過的代碼。我們需要更多信息來幫助您。你使用快遞嗎?如果是,那麼您使用的路線是什麼?你要求什麼信息發送到快遞?你是否發送了帶有請求的用戶名或用戶ID? – Snekw
增加了更多的代碼,謝謝你的 – Ramil