Concat or virtual attributes

33 views
Skip to first unread message

Michal Koudelka

unread,
Sep 2, 2013, 7:25:52 PM9/2/13
to mongo...@googlegroups.com
Hi,

I am sorry for probably very green question, but I am still pretty new to nonsql db world and I am currently thinking what is the best solution for my problem.

Let's assume I have object User. I have two strings attributes, first_name and last_name. Then, I am supposed to search over all users, but only attribute I have is the user input. So, in DB I can have {first_name => "John", last_name => 'Smith'}, but user is looking for 'John Smith'.

In SQL DB, I would just put the columns together by concat, but here I am not sure. Of course, I can search by first AND last name, but I want to get valid results also when users looks for 'John Sm'. Also, in future I would like to search by more attributes, so I am looking for more universal solution.

Thank you for help.

Michael




Chris Heald

unread,
Sep 2, 2013, 7:46:58 PM9/2/13
to mongo...@googlegroups.com
Yes, you'd denormalize those two fields into a single searchable name field. In general, the answer in Mongo is "denormalize all the things!" :D

That said, while Mongo can do search stuff decently well, if you want a really robust search solution, I'd look at using something like ElasticSearch (via the tire gem) or Solr (using sunspot) to do actual indexing and search. Those solutions will be much more robust and extensible for search purposes.

Billy

unread,
Sep 2, 2013, 7:49:43 PM9/2/13
to mongo...@googlegroups.com
For speed I think it's best if you store an extra field that is the concatted version of the user's name, when the record is created/updated. This is a common solution :) There may be a way of doing it with JavaScript or $concat but I haven't heard of a solution that doesn't ignore indexes.



--
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Amruta Krishna R

unread,
Sep 2, 2013, 9:00:32 PM9/2/13
to mongo...@googlegroups.com
Caveat: If we denormalize the fields into a single field, the field will become obsolete if the search criteria changes. If the search criteria changes, you have to update the existing documents with a new field that fits the search criteria.
Reply all
Reply to author
Forward
0 new messages