Is it possible / necessary to index an embedded document in Mongoose?

I have the following schemes; An address is a geocoded location, and the Agency has many addresses. My question is: if I want to be able to perform a geospatial search on the agency based on its addresses, do I need to somehow index them at the agency level (already indexed at the address scheme level)?

In addition, I find it difficult to find information on how to find based on attached documents. In this case, is it possible to do what I hope for, or do I need to expand my domain structure a bit and have "AgencyAddress"? This approach seems a little DBMS for me, but I also see its advantages.

I'm a little confused about how to use ObjectId refs from an agency (or a BridgeAdAdressress object) without an inverted link from an address. I do not want to have backlinks, because the address will be used by a number of other objects in the application.

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} }); 
+6
source share
1 answer

I have no experience with mongoose, so I can explain mongodb indexing in general. From your question, I understand that several geocoded Address documents are built into the Agency.

If you are using mongodb version <= 1.8, it is not possible to index nested geocoded documents. But the function is added from version 1.9. I have been successfully using a couple of months for the same type of circuit. But it was a branch of development (unstable).

Fortunately, version 2.0 was released a week ago. And its stable. For more details, see Link Release Notes 2.0 .

And you cannot index directly embedded documents.

it must be done the same way from the mongodb shell

  db.Agency.ensureIndex({"Address.location": 2d}) 

I don’t know the exact syntax for the mongoose, maybe like

  AgencySchema.index({'Address.location': '2d'}); 

check out mongodb indexing for more info

+11
source

Source: https://habr.com/ru/post/897574/


All Articles