gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:21:09 27 4
gpt4 key购买 nike

(找到答案后,我精简了我的问题,删除了不太相关的部分,以便它对 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

好的,所以集合是空的。然后我使用带有 mongoose 模型的 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 } ] } ]

第一个 Artist 有 3 个文档,它们的 ._id 都与我创建的原始文档不同。

所以这里是我定义 Artist Schema 的地方:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com