Mongoose Foregin Key映射

时间:2017-12-03 04:44:00

标签: javascript angularjs node.js mongodb nodes

我是MongoDB / Mongoose的新手,并试图弄清楚如何映射Schema之间的关系。我不认为问题出在.populate()上。在创建新用户和客户端时,我看不到集合中反映的关系

用户(通过本地/社交网站登录)有许多客户端。 一组客户端属于1个用户

  1. 这是申报外键的正确方法吗? {type:Schema.Types.ObjectId,ref:' Client'}
  2. 两个架构是否应该让彼此的外键相互关联?
  3. 创建用户/客户端是否需要任何其他代码才能显示外键数据?我读了一些关于populate()
  4. 的内容

    用户

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    var userSchema = new Schema({
    
    local: {
        id: String,
        email: String,
        password: String,
        name: String,
        mobile: String,
        clients: {type: Schema.Types.ObjectId, ref: 'Client'}
    
    },
    
    google: {
        id: String,
        token: String,
        email: String,
        name: String,
        clients: {type: Schema.Types.ObjectId, ref: 'Client'}
    
    }
    
    });
    module.exports = mongoose.model('User', userSchema);
    

    客户端

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    
    var clientSchema = new Schema({
    
        id: String,
        firstname: String,
        lastname: String,
        mobile: String,
        user: {type: Schema.Types.ObjectId, ref: 'User'}
    
    });
    
    module.exports = mongoose.model('Client', clientSchema);
    

    创建用户

    app.post("/api/user", function (req, res) {
    const user = req.body;
    console.log(user);
    
    User.findOne({ 'local.email': user.email },
      function (err, result) {
        if (err) {
          console.log(err);
          handleError(err, res);
          return;
        }
        if (result) {
          res.status(500).send("Email already exists in database");
        } else {
          var newUser = new User();
          newUser.local.password = generateHash(user.password);
          newUser.local.email = user.email;
          newUser.local.name = user.name;
          newUser.local.mobile = user.mobile;
          newUser.save(function (err, result) {
            res.status(201).send("User added to database");
          });
        }
    
      });
     });
    

    创建客户端

    app.post("/api/client", function (req, res) {
    const client = req.body;
    console.log(client);
    
    Client.findOne({
      $and: [
             { firstname: client.firstname },
             { lastname: client.lastname }
           ]
    },
      function (err, result) {
        if (err) {
          console.log(err);
          handleError(err, res);
          return;
        }
        if (result) {
          res.status(500).send({msg:"Client already exists in database"});
        } else {
          var newClient = new Client();
          newClient.firstname = client.firstname;
          newClient.lastname = client.lastname;
          newClient.mobile = client.mobile;
          newClient.save(function (err, result) {
            res.status(201).send("Client added to database");
          });
        }
    
      });
    
     });
    

0 个答案:

没有答案