(答えを発見したので、将来の読者にとってより読みやすく/役立つように、関連性の低いビットを削除するために質問を簡素化しました。)
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
次に、マングース モデルを使用して新しいドキュメントを作成します。
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 } ] } ]
最初のアーティストのドキュメントが 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
}]
});
何を与える?