Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用嵌套字段在MongoDB/Mogoose中创建多对多关系?_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

使用嵌套字段在MongoDB/Mogoose中创建多对多关系?

使用嵌套字段在MongoDB/Mogoose中创建多对多关系?,mongodb,mongoose,mongodb-query,Mongodb,Mongoose,Mongodb Query,我目前正试图在mongodb/mongoose中建立多对多关系 我有两种模式: // User Schema var UserSchema = mongoose.Schema({ email: { type: String, index:true }, name: { type: String }, tasks:[{type:mongoose.Schema.ObjectId, ref: 'Tasks'}] }

我目前正试图在mongodb/mongoose中建立多对多关系

我有两种模式:

// User Schema
var UserSchema = mongoose.Schema({
    email: {
        type: String,
        index:true
    },
    name: {
        type: String
    },
    tasks:[{type:mongoose.Schema.ObjectId, ref: 'Tasks'}]
});

这里的想法是,每个用户都可以承担一项任务,并且该任务对于每个用户都有自己的状态(例如,notStarted、inProgress、Complete、Failed)。此状态将随着用户完成任务而更改。每个用户将看到相同的任务(即名称+描述),但与之关联的状态不同。此外,每个任务都有自己的状态(可用、隐藏),这将决定用户是否可以看到任务。这对于每个用户来说都不是唯一的

这是我迄今为止的思考过程:

我想我可以将每个任务的objectid与状态一起嵌套到用户中。例如:

{
   email: "Bob@bob.com"
   name: "Bob",
   tasks: {
     { _id: 001, status: "Active"},
     {_id: 002, status: "inProgress"},
     { _id: 003, status: "Failed"}
   }
 },
 {
   email: "Mary@mary.com"
   name: "Mary",
   tasks: {
     { _id: 001, status: "Failed"},
     { _id: 002, status: "Active"},
     { _id: 003, status: "Active"}
   }
 }
但是,这意味着每当我创建新任务时,我都需要将其添加到所有用户的任务列表中,状态设置为默认值(例如notStarted)。此外,每当我添加新用户时,我需要获取所有任务,并将它们添加到用户的任务列表中

这对我来说似乎有点笨重,我觉得一定有更好的办法。如果这是最好的方法,我不太确定我会用什么来写这篇文章。我在想也许使用或更合适?或者这会不会产生一个可能不是最好的想法,因为我读到的是Mongo反模式

我还发现这有点相关,但这是大约6年前的事了,我想我需要更多的深度或代码示例


任何帮助都将不胜感激

如果要在创建新任务时在用户中添加新任务,则查询应为:

  taskCtr.create = (req, res) => {
  const {
      name,
      description,
      status
  } = req.body;

  TaskSchema.create({
      name,
      description,
      status
  }).then((result) => {
      if (result) {
          UserSchema.update({}, {
              $push: {
                  tasks: {
                      _id: result._id,
                      status: result.status
                  }
              }
          }, {
              multi: true
          }).then((userUpdated) => {
              res.status(200).json({
                  message: 'A new Task created successfully!'
              })
          }).catch((err) => {
              res.status(500).json({
                  error: 'Internal server error!'
              });
          });
      }
  }).catch((err) => {
      res.status(500).json({
          error: 'Internal server error!'
      });
  });
 };

如果所有用户都具有相同的状态,那么您可以应用
$push
,这正是我想要的!谢谢:)后续问题:在创建新用户时,如何将所有任务添加到用户?
  taskCtr.create = (req, res) => {
  const {
      name,
      description,
      status
  } = req.body;

  TaskSchema.create({
      name,
      description,
      status
  }).then((result) => {
      if (result) {
          UserSchema.update({}, {
              $push: {
                  tasks: {
                      _id: result._id,
                      status: result.status
                  }
              }
          }, {
              multi: true
          }).then((userUpdated) => {
              res.status(200).json({
                  message: 'A new Task created successfully!'
              })
          }).catch((err) => {
              res.status(500).json({
                  error: 'Internal server error!'
              });
          });
      }
  }).catch((err) => {
      res.status(500).json({
          error: 'Internal server error!'
      });
  });
 };