私はノードが初めてで、Feathersjs を使用しています。私は mongoose populate を使って と の関係を作ろうとしていusers
ますtasks
私のモデル:
user-model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
email: {type: String, required: true, unique: true},
password: { type: String, required: true },
tasks: [{ type: Schema.Types.ObjectId, ref: 'Task' }],
createdAt: { type: Date, 'default': Date.now },
updatedAt: { type: Date, 'default': Date.now }
});
var Task = mongoose.model('Task', storySchema);
const userModel = mongoose.model('user', userSchema);
module.exports = userModel;
task-model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const taskSchema = new Schema({
title: { type: String, required: true },
_creator : { type: String, ref: 'User' },
createdAt: { type: Date, 'default': Date.now },
updatedAt: { type: Date, 'default': Date.now }
});
var User = mongoose.model('user', storySchema);
const taskModel = mongoose.model('task', taskSchema);
module.exports = taskModel;
新しいタスクを追加すると、user.tasks はまだ空です:
> db.users.find().pretty()
{
"_id" : ObjectId("5832da6919756a0edc2dfc59"),
"email" : "igorpollo@gmail.com",
"password" : "$2a$10$DToGYQ8smdfsK4oJPXmcyOdIfxXEaQGO5P16AhzBlrpESUMt5baNi",
"updatedAt" : ISODate("2016-11-21T11:28:41.371Z"),
"createdAt" : ISODate("2016-11-21T11:28:41.371Z"),
"tasks" : [ ],
"__v" : 0
}
> db.tasks.find().pretty()
{
"_id" : ObjectId("5832da7619756a0edc2dfc5a"),
"title" : "test",
"_creator" : "5832da6919756a0edc2dfc59",
"updatedAt" : ISODate("2016-11-21T11:28:54.470Z"),
"createdAt" : ISODate("2016-11-21T11:28:54.470Z"),
"__v" : 0
}