Reputation: 61
I'm creating a website for a cybercafe and I need to specify that "that particular user" is using "that particular computer". This is what I have done so far:
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'
})
Upvotes: 0
Views: 39
Reputation: 2743
Do do this, I think you can delete your userSchema.virtual
, and update your userSchema to make computer reference on each user like this :
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
});
Then, if you want to find a user with his computer, you can use mongoose populate like this:
UsersModel.find().populate('computerId').exec(function(err, users) {
if (err) throw err;
console.log(users); // display users with affected computers if exists
});
Hope it helps.
Upvotes: 1