Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Can/should I index an embedded document in Mongoose?

Can/should I index an embedded document in Mongoose?

Problem

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}
});
Problem courtesy of: Chance

Solution

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

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

Solution courtesy of: RameshVel

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Can/should I index an embedded document in Mongoose?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×