Mongoose Ref用户对象

时间:2016-12-02 21:46:31

标签: node.js mongodb mongoose

我希望每个人今天都能学到一些东西。我需要一些帮助。我有一个礼物模型,看起来像这样:

   let giftSchema = new mongoose.Schema({
       gift: String,
       date: {
           type: Date
       },
       giftDescription: String,
       giftAmount: String,
       giftCode: String,
       redeemCode: String,
       passCode: String,
       senderFirstName: String,
       senderLastName: String,
       giftMessage: String,
       user: {
           id: {
               type: mongoose.Schema.Types.ObjectId,
               ref: 'User'
           },
           username: String
       }
   });

   module.exports = mongoose.model('Gift', giftSchema);

这是我的用户模型:

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

   let UserSchema = new mongoose.Schema({
       firstName: String,
       lastName: String,
       alaisFirstName: String,
       alaisLastName: String,
       username: String,
       password: String,
       isAdmin: Boolean,
       addressLine1: String,
       addressLine2: String,
       city: String,
       state: String,
       zipCode: String
   });

   UserSchema.plugin(passportLocalMongoose);

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

我有2个用户,没有访客。管理员将礼物发送给用户。所以作为管理员,在新路线上,我需要向用户发送一些内容。但是,在我的字段中: for (想想gmail搜索字段)我将搜索firstName,lastName,alaisFirstName和alaisLastName,而不是将礼物发送给用户。

我该如何建模呢?我不能通过type: mongoose.Schema.Types.ObjectId,来做,因为管理员是将其发送给用户的人,但我想要类似的东西。

0 个答案:

没有答案