title: Fetching collection with backbone.js but id for each model is not set
body: <p>In short, I'm fetching a collection but afterwards I'm unable to get a specific entity from the collection using either its <code>id</code> or <code>cid</code>.</p>
  
  <p>For this problem, consider my two REST endpoints <code>/sites</code> and <code>/sites/some-site-id/entities</code>
  Both <code>sites</code> and <code>entities</code> are collections. Here are my models/collections:</p>
  
  <p><strong>Models / collections</strong></p>
  
  <pre><code>var ROOT = 'http://localhost:5000';
  
  Entity = Backbone.Model.extend({
      idAttribute: "_id",
      defaults: {
          xpos: 10,
          ypos: 10,
          site: "default"
      }
  });
  
  Entities = Backbone.Collection.extend({
      model: Entity,
      ownUrl: '/entities',
      site: {},
      url: function() {
          return ROOT + "/sites/" + (this.site? this.site : 'null') + this.ownUrl;
      },
      initialize: function(models, options) {
          if(!options.site) throw new Error("No site associated with entities");
          if(!options.site.get("_id")) throw new Error("Associated site has no _id");
  
          this.site = options.site.get("_id");
      }
  });
  
  Site = Backbone.Model.extend({
      idAttribute: "_id",
      defaults: {
          name: "default",
          description: "My site"
      }
  });
  
  Sites = Backbone.Collection.extend({
      model: Site,
      url: ROOT + "/sites"
  });
  </code></pre>
  
  <p>My problem is that when I'm fetching the entities for a site, I cannot lookup a certain entity from the collection using the <a href="http://backbonejs.org/#Collection-get" rel="nofollow"><code>get</code></a> method on collection. I simply get <code>undefined</code> returned.</p>
  
  <p>This is how I test it (entities.fetch will get 4 entities):</p>
  
  <p><strong>Test code</strong></p>
  
  <pre><code>var site1 = new Site({id : "52813a2888c84c9953000001"});
  sites.add(site1);
  site1.fetch({success: function(model, response, options) {
      entities = new Entities([], {site: site1});
      entities.fetch({success: function(model, response, options) {
          entities.each(function (entity) {
              console.log("entity.get(\"_id\") = " + entity.get("_id"));
              console.log("entity.id = " + entity.id);
              console.log("Looking up entity using id (" + entity.get("_id") + "): " + entities.get(entity.get("_id")));
              console.log("Looking up entity using cid (" + entity.cid + "): " + entities.get(entity.cid));
              console.log("");
          });
      }});
  }});
  </code></pre>
  
  <p>When I run this, I get:</p>
  
  <p><strong>Test result</strong></p>
  
  <pre><code>entity.id = undefined
  entity.get("_id") = 528146ade34176b255000003
  Looking up entity using id (528146ade34176b255000003): undefined 
  Looking up entity using cid (c1): undefined
  
  entity.id = undefined
  entity.get("_id") = 528146ade34176b255000002
  Looking up entity using id (528146ade34176b255000002): undefined
  Looking up entity using cid (c2): undefined
  
  entity.id = undefined
  entity.get("_id") = 528146ade34176b255000001
  Looking up entity using id (528146ade34176b255000001): undefined
  Looking up entity using cid (c3): undefined
  
  entity.id = undefined
  entity.get("_id") = 528146ade34176b255000004
  Looking up entity using id (528146ade34176b255000004): undefined
  Looking up entity using cid (c4): undefined 
  </code></pre>
  
  <p>What I would expect is for the collection to return the specific entity. Does it have something to do with my special idAttribute "_id" (for compliance with mongodb)?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Apparently it seems to have something to do with my idAttribute. Because if I add an "id" field to every entity returned, I can lookup entities using its id. Like so on server side:</p>
  
  <pre><code>function getEntitiesInSite(siteId, fun) {
      db.siteentities.find({site: mongojs.ObjectId(siteId)}, function(err, entities) {
          entities.forEach(function (entity) {
              entity.id = entity._id;
          });
          fun(err, entities);
      });
  }
  </code></pre>
  
  <p>This isn't exactly what I wanted and I can imagine that I would run into other problems in the future with inconsistent id's (having both <code>id</code> and <code>_id</code> fields).</p>