Published

06 June 2014
 / 

Category

Development
 / 

Tags

It may feel a little dirty to put the same value in both the document id, for example:

id: Users/E41270DC-4E0D-48C2-B3CE-4A51FFB649E3

  {
     "UserId" : "E41270DC-4E0D-48C2-B3CE-4A51FFB649E3",
     "Name" : "User",
     "Email" : "user@server.com",
   }

However, if UserId wasn’t included in the document as a property, then when you go to create an index on Users that needs to load related documents with ids that also contain the user id value, you’ll be in trouble. The closest thing you have to use is the string value of the document id (ie. id: Users/E41270DC-4E0D-48C2-B3CE-4A51FFB649E3). It’s very difficult, if not impossible, to pull the GUID out of the user id string from within the index.

If you do have millions of User documents in your database, and you did not remember to include the UserId property in the document, here’s an easy patch request that will add this UserId property to all of your docs:

  store.DatabaseCommands.UpdateByIndex("Raven/DocumentsByEntityName",
    new IndexQuery { Query = "Tag:Users" },
    new ScriptedPatchRequest
    {
      Script = @"
          var currentDocId = __document_id;
          var id = currentDocId.substring(currentDocId.indexOf('/')+1);
          this.UserId = id;
          "
    }
  );
                


blog comments powered by Disqus