Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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
Node.js 如何使用GraphQL在MERN应用程序中插入MongoDB模式中的数组字段?_Node.js_Reactjs_Mongodb_Express_Graphql - Fatal编程技术网

Node.js 如何使用GraphQL在MERN应用程序中插入MongoDB模式中的数组字段?

Node.js 如何使用GraphQL在MERN应用程序中插入MongoDB模式中的数组字段?,node.js,reactjs,mongodb,express,graphql,Node.js,Reactjs,Mongodb,Express,Graphql,我正在学习如何使用MongoDB、express、react、node和GraphQL进行开发。我做了很多研究,在理解一个概念时遇到了一些问题。我的应用程序基本上是一个菜谱应用程序,用户可以对菜谱进行CRUD,并在线收集菜谱 考虑到这一点,我创建了以下模式: const CookbookProfileSchema = mongoose.Schema({ fName: String, lName: String, email: {type: String, lowercas

我正在学习如何使用MongoDB、express、react、node和GraphQL进行开发。我做了很多研究,在理解一个概念时遇到了一些问题。我的应用程序基本上是一个菜谱应用程序,用户可以对菜谱进行CRUD,并在线收集菜谱

考虑到这一点,我创建了以下模式:

const CookbookProfileSchema = mongoose.Schema({
    fName: String,
    lName: String,
    email: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true},
    password: String,
    cookbook: [{
      recipe: {
        rTitle: String,
        rDescription: String,
        rCookTime: String,
        rIngredients: [{
          iName: String,
          iMeasurement: String,
        }],
        rInstructions: [{
          text: String
        }]
    }
  }]    
});
到目前为止,我只使用过SQL数据库,我发现了一些教程,这些教程向我展示了如何使用关系创建两种不同的模式,但在这一点上,使用关系数据库不是更好吗

以下是我的类型定义:

const typeDefs = `
  type Query {
    userProfiles: [UserProfile]
    userLookup(id: ID!): [UserProfile]
    userLogin(email: String, password: String): [UserProfile]
  }
  type UserProfile{
    id: ID!
    fName: String
    lName: String
    email: String
    password: String
    cookbook: [Recipe]
  }

  type Recipe{
    rTitle: String
    rDescription: String
    rCookTime: String
    rIngredients: [Ingredient]
    rInstrctions: [Instruction]
  }

  type Ingredient{
    iname: String
    iMeasurement: String
  }

  type Instruction{
    text: String
  }

  type Mutation {
    createUser(fName: String, lName: String, email: String, password: String): UserProfile
    removeUser(id: ID!): Boolean
    updateUser(id: ID!, fName: String, lName: String): Boolean
    updatePassword(id: ID!, password: String): Boolean
  }
` 
到目前为止,这些是我的解决方案:

const resolvers = {
  Query: {
    userProfiles: () => UserProfile.find(),
    //userLogin: async (_, {id}) => await UserProfile.findById(new ObjectID({id}))
    userLookup: (_, {id}) => UserProfile.find(ObjectId(id)),
    userLogin: (_, email, password) => UserProfile.find(email, password)
  },
  Mutation: {
    //User CRUD Mutations
    createUser: async (_, {fName, lName, email, password }) => {
      const user = new UserProfile({ fName, lName, email, password, cookbook: null});
      await user.save();
      return user;
    },
    removeUser: async (_, {id}) => {
      await UserProfile.findByIdAndRemove(id);
      return true;
    },
    updateUser: async (_,{id, fName, lName}) => {
      //Code in front end to check if the fname or lname is null
      await UserProfile.findByIdAndUpdate(id, {fName , lName });
      return true;
    },
    updatePassword: async (_, {id, password}) => {
      await UserProfile.findByIdAndUpdate(id, { password });
      return true;
    },

  }
}

const server = new GraphQLServer({ typeDefs, resolvers })
mongoose.connection.once('open', function() {
  server.start(() => console.log('Server is running on localhost:4000'))
});
我打算让一个用户有很多食谱,一个食谱可以有很多成分和很多说明,所以我可以以列表形式显示它,他们可以只检查该会话或状态。在关系数据库中,我会有一个食谱表,并让它们与用户相关。说明和配料将与配方相关。然后,对于视图,我只需查询属于该用户的所有食谱即可制作食谱。我怎么能在这里做类似的事情,或者我应该走关系路线?我真的很想体验如何充分使用mongodb


任何帮助或建议都将不胜感激

我猜您已经发现您没有
中的许多
属于MongoDB中的e.t.c关系方法。但是,您可以使用MongoDB对关系类型关系进行建模


尽管如此,我应该强调,对于像MongoDB这样的NoSQL数据库来说,“拥有尽可能多的表来表示不同的数据实体(数据规范化)”可能不是一个好的模式设计。大多数时候,您最好将彼此相关的文档放在一起。

谢谢您的输入!最后,我混合使用了关系式设置和一些嵌入式对象/数组。我又学到了很多谢谢。