如何在猫鼬中加入两个模型?

时间:2018-07-08 10:36:44

标签: node.js mongodb

我做了这两个模型,我不知道如何在食物和用户之间建立联系。

模型1:

const mongoose = require('mongoose');
const passportLocalMongoose = require('passport-local-mongoose');


var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
    repassword: String,
    email: String

})

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);

模型2:

var mongoose = require('mongoose');


var foodSchema = new mongoose.Schema({
   name: String,
   image: String,
   notify: String,
   createdAt: {type: Date, default: Date.now},
    author: {
      id: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "User"
      },
      username: String
   },
});


   module.exports = mongoose.model("food", foodSchema);

0 个答案:

没有答案