2014년 11월 30일 일요일

Multi collections query

Hi guys I want to search multi collections and fetch all data from those collections if it's suitable for my condition.
Example: 
_ First i want to fetch a list of classes.
_ Second i want to fetch a list of student belong to those classes precedding.
How can i do this in asynchronous way? Below is my code:
classModel.find({}, function(err, data) {
// What i will do next with studentModel
})
I've tried with $in, but can you show me another way?



It isn't possible to query across collections in Mongo. There are no joins. 

Ask yourself, what do you want to show out of the query? How often will the queries need to be carried out? Are participants of a single class going to be a large unbounded array? Will the data be constantly updated, once the classes are created? Will the classes be a huge collection of data? As you answer these and other questions about the data access and usage, you see how one or more collections can, or even should, be formed. For instance, you could theoretically just have a single classparticipation collection, which holds (embeds) user and class data of each class. If student information constantly changes, having common student data all over the classparticipation collection might not be wise. So, you might not want that data in the classparticipation collection, but rather in a separate students collection. The students collection would hold more pertinent and volatile data about each student and there would be a reference to each student in the classparticipation collection. If you need that more pertinent or volatile data, you'd have ids (references) for the student participants in that or those classes. One quick query in the student collection later you'd have this data too. 

It is all up to you and what you need. So it depends on what you want to do and what you need. You'll hear this often with Mongo. This "it depends" is what makes Mongo so powerful and yet sometimes it seems it is limited. And for sure, this "it depends" is often misunderstood and leaves it (and other NoSql technology) open for criticism. It seems people like being boxed in and having to conform to something (like SQL and RDBMSes) than be free to make their own choices, which often can lead to mistakes, even major mistakes. Maybe that is why people are wary to choose MongoDB (or NoSql technology). The risks of having free choices and having to make decisions with those choices can be quite high.:)



You might also want to look at this part of the MongoDB manual. 




Thanks a lot @Scott, i'll ceck it out :D



MongoDB doesn’t support subquery and join, you should first retrieve the data before any further computation can be performed.
Then programming languages like Java and Php have to be used for realizing the operation and the code will be complicated. Try using esProc in this case. 
It is specially designed for structured data computing and produces more concise code than what Java or Php does. For concrete examples, please refer to http://blog.raqsoft.com/?p=1591.
esProc also provides a JDBC interface, though which the access of an esProc program is almost the same as that of a database. 



The example in that article isn't very difficult and doing it in PHP wouldn't be difficult either. The only difference to SQL would be you need two queries.

댓글 없음:

댓글 쓰기