in Mongoose i have two Schemas, which are linked using Mongoose populate:
var SystemSchema = new Schema({
name : {type : String, trim : true}
, hosts : [{ type: ObjectId, ref: 'Host' }]
});
var HostSchema = new Schema({
name : {type : String, trim : true}
});
My question: I have a given ObjectId of a host and now want to see which which Systems do have a reference to that host.
I do this because i normally do access only the SystemsSchema and want to populate the Hosts, but i was asked if i can check the Hosts for Systems as well. Now i tried to simply search and not to populate from Hosts to Systems, but i'm a little stuck.
One of my attempts was to search with where, but this doesn't succeed:
System.where('hosts').in(['4fabca804c9d76ac0b000022']).run(function(err, hosts) {
console.log(hosts);
});
Is there any way to search like this? Please do not tell me to simply put the HostSchema as subdocuments to the SystemSchema, this here is just a simplified example.
Thanks in advance!
Best Regards, Uli
I think you've missed .populate
System
.find({ hosts : '4fabca804c9d76ac0b000022' })
.populate('hosts')
.run(function(err, systems) {
console.log(systems);
});