为什么猫鼬会多次保存同一个文档

Why would Mongoose be saving the same document multiple times?

本文关键字:保存 同一个 文档 为什么      更新时间:2023-09-26

(找到答案后,我减少了我的问题以删除不太相关的部分,以便对任何未来的读者更具可读性/有用性。


我正在猫鼬中测试我的模式/模型,看看它们是否按预期工作。大多数情况下,他们确实如此,除了我发现我有时会多次保存相同的文档。

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

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创建一个新文档:

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修复它。这是一个愚蠢的错误,但我认为它的效果非常令人惊讶,所以这个答案可能对未来的人有用。(有谁知道为什么它会导致这种行为,而不仅仅是抛出错误或其他东西?