Mongo and removing spaces from @mentions


#1

I got a simple mentioning script, it adds a link to the users profile if that user exists.

content = content.replace(/@(\w+)?/g, function(match, contents, offset, content) {
	contents = contents.replace(/\s/g, ''); // remove spaces from the mention
	if(Meteor.users.find({username: contents}).count() > 0) {
		return "<a href=''>@"+Meteor.users.findOne({username: contents}).username+"</a>";
	} else {
		return contents;
	}
});

The problem with this though is that I don’t know how to make mongo search for the string with the removed spaces. If I use a “like” query, then it will find multiple results. I was thinking of maybe storing the original string in another variable and search with that, but that just brings me back to square one: mongo would have to search for all possible results which is very bad.

Any ideas?


#2

It’s not very clear from your description: since what you get back is a presumably valid username, once the spaces have been removed, why would you still need to perform a regex search (“like”) ?

The username is supposed to be unique, so the query should either return one document, or null.