Arrays 基于阵列逻辑的Mongoose模型
我目前正在接受猫鼬的培训,为此我正在开发一个小的消息应用程序 我有一个用户集合,这里是user.js:Arrays 基于阵列逻辑的Mongoose模型,arrays,node.js,mongodb,express,mongoose,Arrays,Node.js,Mongodb,Express,Mongoose,我目前正在接受猫鼬的培训,为此我正在开发一个小的消息应用程序 我有一个用户集合,这里是user.js: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var UserSchema = new Schema({ username: String, password: String }); module.exports = mongoose.model('User', UserSchema); var
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
password: String
});
module.exports = mongoose.model('User', UserSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ChatroomSchema = new Schema({
name: String,
password: String,
members: ???
});
module.exports = mongoose.model('Chatroom', ChatroomSchema);
router.route('/chatroom')
.post(function(req, res) {
var chatroom = new Chatroom();
chatroom.name = req.body.name;
chatroom.password = encrypt(req.body.password,chatroom.name);
chatroom.members = ???;
chatroom.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Chatroom created!' });
});
});
现在我想写一个聊天室模型,它应该包含一个用户列表,我应该如何继续?这是我当前的聊天室.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
password: String
});
module.exports = mongoose.model('User', UserSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ChatroomSchema = new Schema({
name: String,
password: String,
members: ???
});
module.exports = mongoose.model('Chatroom', ChatroomSchema);
router.route('/chatroom')
.post(function(req, res) {
var chatroom = new Chatroom();
chatroom.name = req.body.name;
chatroom.password = encrypt(req.body.password,chatroom.name);
chatroom.members = ???;
chatroom.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Chatroom created!' });
});
});
最后,我想知道在创建聊天室时如何在该成员数组中发布用户,以下是我的/chatroom路由代码:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
password: String
});
module.exports = mongoose.model('User', UserSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ChatroomSchema = new Schema({
name: String,
password: String,
members: ???
});
module.exports = mongoose.model('Chatroom', ChatroomSchema);
router.route('/chatroom')
.post(function(req, res) {
var chatroom = new Chatroom();
chatroom.name = req.body.name;
chatroom.password = encrypt(req.body.password,chatroom.name);
chatroom.members = ???;
chatroom.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Chatroom created!' });
});
});
是否有特定的方法将用户绑定到members数组,或者我应该将members定义为字符串数组:
members : {username:String}
尝试引用文档 聊天室.js
var ChatroomSchema = new Schema({
name: String,
password: String,
members: [Schema.Types.ObjectID, ref = 'user']
});
控制器
router.route('/chatroom')
.post(function(req, res) {
var chatroom = new Chatroom();
chatroom.name = req.body.name;
chatroom.password = encrypt(req.body.password,chatroom.name);
chatroom.members = req.user; // if the user is authenticated
chatroom.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Chatroom created!' });
});
});
在你的情况下,人口将是理想的。从: 填充是自动替换指定对象的过程 文档中包含来自其他集合的文档的路径 Population将无缝地帮助您将
用户
集合中的数据带入您的聊天室
模型。考虑下面的例子来说明这种方法:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ChatroomSchema = new Schema({
name: String,
password: String,
members: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
module.exports = mongoose.model('Chatroom', ChatroomSchema);
在聊天室模式定义中,通过将字段定义成员
括在方括号中,向数据中引用为\u id
的每个对象(称为ObjectId)添加主代理键列表,以获得此功能。这些键将用于引用用户
集合中的文档
注意:-定义引用时,ref
属性必须与模型定义中的模型名称完全匹配,否则将出现错误(假设您引用的是不存在的成员
模型或用户
)
读取数据
这就是猫鼬种群通过其功能使文档读取变得非常简单和快速的地方。因此,例如,要在聊天室中显示引用的用户
,请使用字符串中该字段的名称作为参数调用方法,例如
Chatroom.findOne({"_id": chatId})
.populate("members")
.exec(err, chats) { ... }
写入数据
保存聊天室
模型的数据时,还需要保存对用户的引用。例如,当创建一个新的聊天室
时,您需要一个用户
引用数组保存为成员字段:
router.route('/chatroom')
.post(function(req, res) {
var chatroom = new Chatroom();
var usersIds = ["54b5659536cd5250a3a93bd3", "54b5659836cd5250a3a93bd4"]; // <-- an array with members ids, for example.
chatroom.name = req.body.name;
chatroom.password = encrypt(req.body.password,chatroom.name);
chatroom.members = usersIds;
chatroom.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Chatroom created!' });
});
});
router.route(“/chatroom”)
.post(功能(请求、恢复){
var chatroom=新聊天室();
var usersIds=[“54B565953CD5250A3A93BD3”,“54B565983CD5250A3A93BD4”];//谢谢您的回答,这非常有帮助!我正在使用令牌身份验证,目前正在令牌中存储用户名。我正在考虑存储用户ObjectID,以避免在定义成员数组中的第一个用户时必须再次请求获取用户名ID。您知道有没有好的prac关于这一点,你几乎可以在你的成员字段中使用任何数据类型,只要它是被引用模型的唯一标识符,因此在这个例子中,它不仅限于ObjectId
,而且你可以使用用户名作为引用,例如成员:[{type:String,ref:'User'}]
,然后在保存聊天室实例时只需保存用户名数组。因此,当您可以只使用用户名时,也可以省去将用户的ObjectId
存储在令牌中的麻烦(用户名也是唯一的)。这是另一种选择,但通常取决于您的应用程序需要。在这种情况下,填充仍然有效吗?如果我使用用户名而不是ObjectID?我现在有此错误(请求的JSON结果):{“message”:“Cast to ObjectID”路径\“id\”,“name”:“CastError”,“kind”:“ObjectID”处的值\“test\”失败,“value”:“test”,“path”:“_id”}
这是我的请求:.get(函数(req,res){Chatroom.find({'members':req.decoded.username})。填充(“members”).exec(函数(err,chatrooms){…})
忘了提到,如果你想使用用户名
作为你的用户参考,那么你必须在用户
模式定义中将\u id
定义为字符串
,因为默认情况下Mongoose将\u id
字段视为对象id
。好的,所以实际上我不能使用我们需要的用户名e _id,对吗?所以我必须在这个_id字段中存储用户名,而不是我创建的自定义用户名字段?