あなたのスキーマ内のイベントのキーを追加します。上記のコードは、あなたが2つのスキーマ、EventDataのためのユーザと他のための1つを作成することができます動作しない場合
var userSchema = new Schema({
user: { type: String, required: true, trim: true },
password: { type: String, required: true, trim: true },
events: [{
notes: { type: String,required: true, trim: true },
start_date: { type: Date,required: true },
end_date: { type: Date,required: true }
}]
}
userSchema.plugin(AutoIncrement);
var userSchema = mongoose.model('userSchema', userSchema);
});
、そしてuserSchemaであなたのEVENTDATAを移入することができます。そして、あなたのスキーマがそのようになります。
ので、あなたのコードはそのようになります:
userSchema.js:
var userSchema = new Schema({
user: { type: String, required: true, trim: true },
password: { type: String, required: true, trim: true },
events: {type: mongoose.Schema.Types.ObjectId, ref: 'EventData' }
userSchema.plugin(AutoIncrement);
module.exports = mongoose.model('userSchema', userSchema);
});
そして、あなたのeventDataSchemaは次のようになります。
eventSchema.js:
var eventDataSchema = new Schema({
notes: { type: 'string',required: true, trim: true },
start_date: { type: Date,required: true },
end_date: { type: Date,required: true }
}
eventDataSchema.plugin(AutoIncrement);
module.exports = mongoose.model('EventData', eventDataSchema);
});
、その後、あなたがそのような結果を得ることができます: index.js:
var eventSchema = require('./eventSchema');
var userSchema = require('./userSchema');
var populate = [{
path: 'events',
model: 'EventData',
select: '_id notes start_dat end_date'
}];
var find = function (query) {
return userSchema.find(query).populate(populate).exec();
}
console.log(find());
結果:
{
_id:cfgvhbjnkmkdcfxghgjklxnmbxhdhjxjhjhgx,
user: John Doe,
password: 123,
events: [ { _id: 1gfye56785g3ycgevhxeftx568765egcd,
notes: Event A,
start_date: 1/1/01,
end_date: 1/1/01
} ]
}
あなたは、より具体的に説明してくださいことはできますか? –
確かに...このスキーマには、認証に使用するユーザーとパスワードを実装したいのですが、このスキーマ内では、eventDataのブロックもたくさん必要です。例: ユーザー:ジョン・ドウ パスワード:123の イベント:[{_id:1 ノート:イベントA START_DATE:1/1/01、 END_DATE:1/1/01、 }、 {_id:2 ノート:イベントB START_DATE:1/2/01、 END_DATE:1/2/01、 }、 {_id:3 ノート:イベントC START_DATE:1/3/01、 END_DATE:1/3/01、 } – user258813
John DoeがサインアップしてイベントA、B、Cにアクセスしました。イベントのコレクションが異なる別のユーザーがいるかもしれませんが – user258813