I have the following schemas; Address is a geocoded location and Agency has many addresses. My question is, if I want to be able to perform a geospatial search on agency based on its addresses then do I need to somehow index them at the agency level (already indexed at the address schema level)?
Also, I'm having a hard time finding information regarding how to find based on nested documents. In this scenario, is it even possible to do what I am hoping for or should I expand out my domain structure a bit and have an "AgencyAddress"? This approach seems a bit RDBMS to me but I can also see the advantages of it.
I'm a bit confused about how I could use ObjectId refs from Agency (or a bridge object AgencyAddress) without an inverted link back from address. I don't want to have back links because address will be used by a number of other objects in the app.
Thanks!
var AddressSchema = new Schema({
name : {type: String, default : ''},
street1 : {type: String, default : ''},
street2 : {type: String, default : ''},
city : {type: String, default : '', required: true},
state : {type: String, required : true},
zip : {type: String, default ''},
country : {type: String},
location : {longitude: Number, latitude:Number}
type : {type: String, enum:['agent', 'agency', 'registrant'], index:true}
created_at : {type: Date, default: Date.now},
updated_at : {type: Date, default: Date.now}
primary : {type: Boolean, default: false}
});
AddressSchema.index({location: '2d'});
Agency:
var AgencySchema = new Schema({
name : {type : String, default : '', required : true},
Type : {type : String, enum['medical', 'disaster-service', 'local-law', 'state-law', 'federal-law'], index:true},
Addresses : [Address],
created_at : {type : Date, default : Date.now},
updated_at : {type : Date, default : Date.now}
});
I dont have experience with the mongoose, so i can explain mongodb indexing in general. From your question i understand that multiple Address
geocoded docs embedded into Agency.
If you are using mongodb version <=1.8, its not possible to index the nested geocoded documents. But the feature is added from version 1.9 . I have been successfully using for couple of months for the same kind of schema. But it was a development(unstable) branch.
luckily version 2.0 is released a week ago. And its stable. Check out the link 2.0 Release Notes for more info.
And you cannot index directly on embedded documents.
it has to be done like this from mongodb shell
db.Agency.ensureIndex({"Address.location": 2d})
I don't know the exact syntax for mongoose, may be like this
AgencySchema.index({'Address.location': '2d'});
check out the mongodb indexing for more info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With