2017-12-20 1 views
0

나는 cybercafe 용 웹 사이트를 만들고 있는데, "특정 사용자"가 "특정 컴퓨터"를 사용하도록 지정해야합니다.활성 사용자와 컴퓨터 간의 상호 작용 - 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 
}); 

는 도움이되기를 바랍니다.

관련 문제