0

I am trying to implement a basic search bar functionality in my app.

I have a bunch of articles, and everyone has an owner. This is specified by the userId in the user parameter of each article.

Right now I can search for keywords within the article, the title, and the date. However I want to be able to query for the username of the author of the article, but I only have the userId available to me...

var keyword  = Session.get("search-query");
var query = new RegExp( keyword, 'i' );

var results = Articles.find( { $or: [{'user': query}, // this is only searching userId of the author!
                            {'title': query},
                            {'articleText': query},
                            {'datetime': query}] } );
return {results: results};

I'm not sure how to do this...

3 Answers 3

2

Welcome to non-relational databases! You're coming from an RDBMS environment so you expect joins. There are none. Store the username in the article it belongs to, then yes, if a user change their username, you'll need to loop through the collection and update the username where the _id matches.

Please look here for mongo strategies (this isn't Meteor-specific at all): http://docs.mongodb.org/manual/core/data-modeling/

PS: If you get eye haemorrhaging whenever you look at your schema-which-actually-really-isn't-one, you can still give https://github.com/erundook/meteor-publish-with-relations a go - but note that under the hood it's going to get even 'worse' (have a look at your mongo opslog). Publish with relations is only there for convenience and ease of programming, not performance.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

So I guess you'd have to either save the username as well as it's own field (or put both id and username in a user object), or search the Users collection for valid usernames, and when you found one use its id to search the Articles DB.

The first suggestion would probably be way more efficient I guess.

3 Comments

I wouldnt want to store the username, but the user may change his username... thats be benefit of having an _id that never changes...
And what about the other way I suggested?
I could do multiple searches and combine them. For example query Articles as I have. Then query users. Grab those userId's, and query Articles again for users matching those userId's and merging the results. That just doesnt seem very elegant. I will have repeat search results and I would have to do 3 queries. I'd really like to do it with one query but I'm not sure thats possible...
0

That's pretty easy, assuming you're doing the search on the server which has access to all the users:

var username = Meteor.users.findOne(userId).username;

1 Comment

I am trying to find Articles authored by a user with some username matching a regex query. I'd like to have some sort of functionality where I can look up the user's username from the userId directly within the Articles.find search query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.