【发布时间】:2018-05-02 22:02:07
【问题描述】:
我正在制作一个投票应用程序,我想保存根据用户 ID、IP 地址或 MAC 地址对投票进行投票的用户。
我认为在我的 Poll 模型中添加 voted: VotedSchema 属性会很简单,但由于某种原因它无法正常工作。
投票模型:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const OptionSchema = require('./Option');
const VotedSchema = require('./Voted');
const pollSchema = new Schema({
title: String,
options: [OptionSchema],
dateCreated: { type: Date, default: Date.now() },
_user: { type: Schema.Types.ObjectId, ref: 'User' },
voted: VotedSchema
});
mongoose.model('polls', pollSchema);
投票子文档:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const votedSchema = new Schema({
userID: [String],
IPaddress: [String],
MACaddress: [String]
});
module.exports = votedSchema;
另一方面,如果我不使用子文档,那么它就可以正常工作:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const OptionSchema = require('./Option');
const VotedSchema = require('./Voted');
const pollSchema = new Schema({
title: String,
options: [OptionSchema],
dateCreated: { type: Date, default: Date.now() },
_user: { type: Schema.Types.ObjectId, ref: 'User' },
voted: {
userID: [String],
IPaddress: [String],
MACaddress: [String]
}
});
mongoose.model('polls', pollSchema);
我在这里错过了一些超级简单的东西吗?这个问题让我有点困惑。
【问题讨论】:
-
您能否澄清一下,在引用的子文档的情况下不工作是什么意思?
标签: node.js mongoose model subdocument