mongoose schema field associate array

时间:2015-08-25 05:29:16

标签: mongoose associative-array

我的架构如下:

var Article = new mongoose.Schema({ sentenceArray: {} });

拥有sentenceAudioArray: {}的目的是让我可以在文章中添加{sentId,sentText}个元组。这可以通过标准数组(sentenceArray: [])来实现,但检索不会是O(1)(即,我知道一个sentId,我不能简单地调用sentenceArray[sentId]来检索)。

在我的createArticle方法中,我有

newArticle.sentenceArray = {};

这似乎没有在mongo中创建字段。当我查看数据库时,不会创建此字段sentenceArray。因此,当我做

anArticle.sentenceArray[sentId] = "Some Sentence"

我收到一条错误,即sentenceArray未定义。

有关如何在Mongoose模式中使用关联数组的任何建议。

编辑:请注意,目标是能够在sentenceArray中添加多个句子。我希望打电话给

anArticle.sentenceArray["firstSent"] = "Some Sentence";

anArticle.sentenceArray["secondSent"] = "Another Sentence";

anArticle.sentenceArray["thirdSent"] = "Yet Another Sentence";

以后再打电话

console.log(anArticle.sentenceArray["thirdSent"])

0 个答案:

没有答案