Order By calculated property

59 views
Skip to first unread message

Jon Wynveen

unread,
Jul 9, 2012, 5:25:06 PM7/9/12
to rav...@googlegroups.com
Is there any way to order a query by a property that isn't stored in Raven (as I write this, it sounds pretty obvious that Raven can't query something it doesn't know about)?

What I'm trying to do is sort by the numeric ID instead of the full string ID. I have an Id property on my model that is the full string ID (e.g. "users/123"), and then a property called UserId that parses out just the integer value from the string ID (e.g. 123). How can I sort by the UserId? Right now it's decorated with [JsonIgnore] so that it doesn't get stored (it feels like storing duplicate data, so I didn't want to store it). Do I need to actually store it in order to sort by it?

Here is my User model:

public class User
{
public User()
{
Status = UserStatus.Active;
}
public string Id { get; set; }
[JsonIgnore]
public int UserId
{
get
{
int userId;
int.TryParse(Id.Replace("users/", ""), out userId);
return userId;
}
}
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
public bool OptIn { get; set; }

public DateTime LastActivity { get; set; }
public UserStatus Status { get; set; }
public bool IsDeleted { get; set; }

public string[] RoleIds { get; set; }

[JsonIgnore]
public string FullName
{
get {
return !string.IsNullOrEmpty(FirstName) || !string.IsNullOrEmpty(LastName)
      ? string.Format("{0} {1}", FirstName, LastName)
      : null;
}
}
}

Matt Warren

unread,
Jul 9, 2012, 5:34:32 PM7/9/12
to rav...@googlegroups.com
I think you answered your own question as you asked it ;-)

You can only sort on fields that are stored in the index, because sorting happens at query time.

Jon Wynveen

unread,
Jul 9, 2012, 5:39:47 PM7/9/12
to rav...@googlegroups.com
Yeah, you're probably right that I answered my own question. I guess I'm kinda wondering if it's common or recommended to store the integer version of the ID in the document.

Matt Warren

unread,
Jul 9, 2012, 5:57:49 PM7/9/12
to rav...@googlegroups.com
I don't think it's common, because you don't normally sort by id, you normally have another fields you sort by (name, age, cost etc). And I can't think of a scenario where you'd need to store the integer portion, other than to sort on it.

Kijana Woodard

unread,
Jul 9, 2012, 6:54:16 PM7/9/12
to rav...@googlegroups.com
If you're using HiLo Ids, the sorting won't necessarily be "in order" either. Web farm or multiple desktop clients will pretty much ensure they are out of time order. If you're looking for creation order, there's a forum post about sorting by the metadata field, or you could of course add your own datetime property.

Jon Wynveen

unread,
Jul 10, 2012, 9:19:08 AM7/10/12
to rav...@googlegroups.com
I forgot about the HiLo ids that won't be exactly in order. I already have some date properties on the object, so I'll probably sort on those instead. Thanks.
Reply all
Reply to author
Forward
0 new messages