2017-12-20 12 views
0

私はサイバーカフェ用のウェブサイトを作成しており、 "その特定のユーザ"が "その特定のコンピュータ"を使用していると指定する必要があります。アクティブなユーザとコンピュータのやりとり - MongoDB/Nodejs

var computerSchema = mongoose.Schema({ 
    name: String, 
    type: String, 
    gpu: String, 
    cpu: String, 
    memory: String, 
    cm: String, 
    usedBy : {type: String, default: null}, 
    active: {type: Boolean, default:false, ref:'user'} 

}); 



var userSchema = mongoose.Schema({ 

    firstname: String, 
    lastname: String, 
    age: Number, 
    address: String, 
    mail: String, 
    password: String, 
    status: String, 
    here: {type: Boolean, default:false}, 
    created_date: Date, 
    updated_date: Date, 
    active_hash: String, 
    role_id: { type: Number, default: 2 } 
}); 

userSchema.virtual('users', { 
    ref: 'computer', 
    localField:'_id', 
    foreignField:'active' 
}) 

答えて

1

がこれを行うのですか、私はあなたがあなたのuserSchema.virtualを削除することができると思うし、このような各ユーザーにコンピュータの参照を作るためにあなたのuserSchemaを更新:

var userSchema = mongoose.Schema({ 
    firstname: String, 
    lastname: String, 
    age: Number, 
    address: String, 
    mail: String, 
    password: String, 
    status: String, 
    here: {type: Boolean, default:false}, 
    created_date: Date, 
    updated_date: Date, 
    active_hash: String, 
    role_id: { type: Number, default: 2 }, 
    computerId: {ref: 'computer'} // Your new reference to a computer _id 
}); 

これは私がこれまでにやっていることですあなたは自分のコンピュータでユーザーを検索する場合次に、あなたはこのようにmongoose populateを使用することができます。

UsersModel.find().populate('computerId').exec(function(err, users) { 
    if (err) throw err; 
    console.log(users); // display users with affected computers if exists 
}); 

それがお役に立てば幸いです。

関連する問題