title: Geospatial Indexing with a simple key first
body: <p>After reading about MongoDB and <a href="http://www.mongodb.org/display/DOCS/Geospatial+Indexing" rel="nofollow">Geospatial Indexing</a></p>
  
  <p>I was amazed that it did not support compound keys not starting with the 2d index.</p>
  
  <p>I dont know if I would gain anything on it, but right now the mssql solution is just as slow/fast.</p>
  
  <pre><code>SELECT TOP 30 * FROM Villages WHERE SID = 10 ORDER BY (math to calc radius from the center point)
  </code></pre>
  
  <p>This works, but is slow because it not smart enough to use a index so it has to calc the radius for all villages with that SID.</p>
  
  <p>So in Mongo I wanted to create an index like: <code>{sid: 1, loc: "2d"}</code> so I could filter out alot from the start.</p>
  
  <p>I'm not sure there are any solutions for this. I thought about creating a collection for each <code>sid</code> since they don't share any information. But what are the disadvantages of this? Or is this how people do it ?</p>
  
  <p><strong>Update</strong></p>
  
  <p>The maps are flat: 800, 800 to -800,-800, villages are places from the center of the map and out. There are about 300 different maps which are not related, so they could be in diff collections, but not sure about the overhead.</p>
  
  <p>If more information is need, please let me know.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <pre><code>&gt; var res = db.Villages.find({sid: 464})
  &gt; db.Villages.find({loc: {$near: [50, 50]}, sid: {$in: res}})
  error: { "$err" : "invalid query", "code" : 12580 }
  &gt;
  </code></pre>
  
  <p>Also tried this</p>
  
  <pre><code>db.Villages.find({loc: {$near: [50, 50]}, sid: {$in: db.Villages.find({sid: 464}, {sid: 1})}})
  error: { "$err" : "invalid query", "code" : 12580 }
  </code></pre>
  
  <p>I'm not really sure what I'm doing wrong, but its probably somthing about the syntax. Confused here.</p>