【发布时间】:2016-04-12 10:50:49
【问题描述】:
我正在开发一个使用 KeystoneJS 构建的网站,该网站允许用户发布单词并从其他用户那里获得建议的同义词。单词作为短语或句子的一部分提交,例如“这只猫 [危险地] 差点打翻玻璃。”
我的 Sentence 模型如下所示:
Sentence.add({
sentence: { type: Types.Text, required: true, initial: "New Sentence", index: true },
word: { type: Types.Relationship, ref: 'Word', required: true, index: true, unique: true, initial: true },
submitter: { type: Types.Relationship, ref: 'User', required: true, index: true, unique: true, initial: true },
source: { type: Types.Text },
createdAt: { type: Date, default: Date.now }
});
我尝试根据 Mongoose 文档使 Word 模型独一无二:
var Word = new keystone.List('Word', {
map: { name: 'word' },
_id: { from: 'word', path: 'word', unique: true, fixed: false}
});
Word.add({
word: { type: Types.Text, required: true, initial: "New word", index: true }
});
但如果我通过提交具有相同单词的两个句子来测试它,它只会使用 _id [word]-1、[word]-2 等生成该单词的第二个实例。
我需要能够查询所有使用特定单词的句子,所以我真的需要每个单词一个项目。但是对于我的生活,我无法弄清楚如何使一个领域独一无二。
当我从负责接受 AJAX 请求的路由中添加新 Word 时,我的问题可能是:
var newWord = new Word.model({
word: req.body.word // read from the input box on the home page
});
newWord.save(function(err) {
if (err) {
console.error(err);
}
});
但我认为.save 只会更新现有的唯一字段?
【问题讨论】:
标签: node.js mongodb mongoose keystonejs nosql