javascript - 为什么 Mongoose 会多次保存同一个文档?

标签 javascript mongodb mongoose

(发现答案后,我简化了我的问题,删除了不太相关的部分,以便对 future 的读者来说更具可读性/有用。)


我正在 Mongoose 中测试我的架构/模型,看看它们是否按预期工作。大多数情况下,他们确实这样做,但我发现有时我会多次保存相同的文档。

我将逐步介绍我正在尝试的内容。首先,我加载模型并确认集合为空:

var Artist = require('model-Artist');
Artist.find({}, function(err, docs) {
    docs.forEach(function(doc) { doc.remove(); }); // delete all docs to be safe
});
Artist.count({}, console.log); // => null 0

好吧,所以集合是空的。然后我使用 new 和 mongoose 模型创建一个新文档:

var nedRorem = new Artist();
nedRorem.names.push({
    sort: 'Rorem, Ned',
    display: 'Ned Rorem',
    brief: 'Rorem',
    personal: true
});
nedRorem.born = { year: 1923, month: 10, date: 23 };
nedRorem.places.push('USA');

此时我检查文档的外观:

> nedRorem
{ born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time),
  _id: 522a0694e9a7813c23000020,
  specialGenres: [],
  seeAlso: [],
  memberOf: [],
  places: [ 'USA' ],
  ensemble: false,
  names:
   [ { sort: 'Rorem, Ned',
       display: 'Ned Rorem',
       brief: 'Rorem',
       _id: 522a0694e9a7813c23000035,
       index: true,
       personal: true } ] }

看起来不错。请注意 _id。然后我保存并检查计数:

nedRorem.save();
Artist.count({}, console.log); // => null 1

太棒了!让我们开始创建另一位艺术家:

var catharineCrozier = new Artist();
catharineCrozier.names.push({
    sort: 'Crozier, Catharine',
    display: 'Catharine Crozier',
    personal: true
});

请注意,我还没有保存新的。但现在有多少艺术家?

Artist.count({}, console.log); // => null 3

!!!???那么他们是谁?

Artist.find({}, console.log);
> null [ { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time),
    _id: 522a08a7af52934c1200000a,
    __v: 0,
    specialGenres: [],
    seeAlso: [],
    memberOf: [],
    places: [ 'USA' ],
    ensemble: false,
    names:
     [ { sort: 'Rorem, Ned',
         display: 'Ned Rorem',
         brief: 'Rorem',
         _id: 522a08a7af52934c1200000b,
         index: true,
         personal: true } ] },
  { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time),
    _id: 522a08a8af52934c1200000d,
    __v: 0,
    specialGenres: [],
    seeAlso: [],
    memberOf: [],
    places: [ 'USA' ],
    ensemble: false,
    names:
     [ { sort: 'Rorem, Ned',
         display: 'Ned Rorem',
         brief: 'Rorem',
         _id: 522a08a8af52934c1200000e,
         index: true,
         personal: true } ] },
  { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time),
    _id: 522a08a8af52934c12000010,
    __v: 0,
    specialGenres: [],
    seeAlso: [],
    memberOf: [],
    places: [ 'USA' ],
    ensemble: false,
    names:
     [ { sort: 'Rorem, Ned',
         display: 'Ned Rorem',
         brief: 'Rorem',
         _id: 522a08a8af52934c12000011,
         index: true,
         personal: true } ] } ]

第一个艺术家有三个文档,它们的 ._id 都与我创建的原始文档不同。

这就是我定义艺术家架构的地方:

var artistSchema = new mongoose.Schema({
    names: [{
        sort: String,
        display: String,
        brief: String,
        nonLatin: String,
        personal: { type: Boolean, default: false },
        index: { type: Boolean, default: true }
    }],

    born: ucaDate(true),
    died: ucaDate(),

    ensemble: { type: Boolean, index: true, default: false },
    places: { type: [ String ], index: true },
    memberOf: [ { type: ObjectID, ref: 'Artist' } ],
    seeAlso: [ { type: ObjectID, ref: 'Artist' } ],

    specialGenres: [{
        name: String,
        parent: String,
        classical: Boolean
    }]
});

什么给出了?

最佳答案

所以,我明白了!我的模式中有一个名为“index”的属性:

names: [{
    sort: String,
    display: String,
    brief: String,
    nonLatin: String,
    personal: { type: Boolean, default: false },
    index: { type: Boolean, default: true }
}],

index 在 Mongoose 中用于指示文档属性是否应该由 MongoDB 索引,我在这里将其用作普通文档属性的名称。将属性名称更改为 showInIndex 修复了该问题。这是一个愚蠢的错误,但我认为它所产生的效果是相当令人惊讶的,所以这个答案可能对将来的某人有用。 (有谁知道为什么它会导致这种行为,而不仅仅是抛出错误或其他东西?)

关于javascript - 为什么 Mongoose 会多次保存同一个文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18662841/

相关文章:

javascript - 正则表达式匹配一个数字序列和一个字符。它们之间

javascript - 防止 ReactJS 输入元素中的表单订阅

javascript - 使用 preg_match 从 html 中提取 JS

java - mongoDB将特定数据添加到数据库副本

arrays - MongoDB 从对象数组中的数组中删除一个项目

node.js - Mongo/Mongoose 聚合 - $redact 和 $cond 问题

javascript - 如何计算 Protractor 中下拉选项的总数

linux - 强制 MongoDB 预取内存

javascript - 更改 MongoDB 文档/对象中的所有 MongoDB 对象 ID

node.js - 如何在 Mongoose 模式中具有自动计算的属性