E
E
ekopach2014-02-19 16:47:49
MongoDB
ekopach, 2014-02-19 16:47:49

Mongoose: count to linked collections

Good day to all!
I am learning Node.JS, for the first project I chose the Express + Mongoose bundle, and I have already encountered a number of problems.
There are 2 models:

var Institution = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Поле "Название заведения" не может быть пустым']
    }
});
module.exports = mongoose.model('institution', Institution);

var Faculty = new mongoose.Schema({
    institution: {
        type: mongoose.Schema.ObjectId,
        ref: 'institution'
    },
    name: {
        type: String,
        required: [true, 'Поле "Название факультета" не может быть пустым']
    }
});
module.exports = mongoose.model('faculty', Faculty);

To get all the faculties that belong to an educational institution, I do this:
Faculty.find({institution: institution._id}, function(err, faculties) {
 // ...
        });

But now I need to display a list of educational institutions, and on the contrary, their number of faculties.
Can anyone suggest the best way to do it?
So far, these thoughts:
1. Create a __facultiesCount field in the Institution schema and increase this field when adding a new faculty, and decrease it by 1 when removing it, but it turns out ugly and a lot of extra code :) + the data may not be relevant if, for example, I'm in the database hands I will delete one faculty.
2. After selecting the list of educational institutions - go through them and for each count how many faculties each time - but this is not optimal.
Maybe there is some easier way?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
_
_ _, 2014-02-19
@AMar4enko

Do you need faculties separate from institutes?
If not, then shove the faculties inside the institutes. Then, when selecting institutes, you will immediately have an array of faculties.
In case the collections are different, there are no options other than as you described.
With the __facultiesCount field, a normal solution. Make a separate method for recalculating quantities and do not delete by hand.

D
Dmitry Gusev, 2014-02-19
@illi

Try populate, instead of IDs of institutions in the faculty, it will substitute the objects of institutions from another table
Faculty.find({institution: institution._id}).populate('institution').exec( function(err, faculties) {
// ...
} );

I
Ivan Starkov, 2014-02-28
@icelaba

field __facultiesCount almost without options

K
kstyle, 2015-05-04
@rish_the_human

Hello. Take books on mathematics and algorithms - and go ahead to solve / read / program. And all these beautiful words "I would like to work in the field related to artificial intelligence" are just words. You don't even understand what's behind them. Plus a guarantee that tomorrow you will like iron, the day after tomorrow - 3D graphics, and watch "Terminator" - you want to plunge into robotics. That is, your desires are like the wind at that age - I would not pay attention to them. I am sure, because if you understood what it is to work with AI, you would have long known where to go.

B
bobrovskyserg, 2015-05-04
@bobrovskyserg

> difficult choice of higher education institution
Need learning words conjugation in a sentence.

E
Eserchik, 2015-05-05
@Eserchik

IP is programming with deep use of a mat. Apparatus
Accordingly, either to the faculty of programming or to the mat. faculty.
All graduates have a common misconception: "Now let's go to college and they will teach us there!"
In institutes they are not engaged in training, in institutes they are engaged in teaching the subject.
The quality of teaching is also very lame, because the theory of several books is spread over several years. If we take programming, then many teachers themselves have never written anything more or less complicated in their lives, but operate only with theoretical knowledge. That is, there is no real experience, so there is nothing to share.
Your knowledge and skills are only in your hands:
Take any programming language and books on mathematics and go.
Write, write and write programs again, theory without practice is nothing.
+ There is a nuance that will definitely contribute to the education process:
Drinking will begin from the second year - partying, girls, first job, etc.)

U
UA3MQJ, 2015-05-06
@UA3MQJ

There is a specialty "Specialty 220100 Computers, complexes, systems and networks".
It's somewhere between programming and electronics. We were taught to develop computer architecture and software for it. Some basic knowledge about FPGAs. I think it's not the best thing for an AI programmer to be a pure programmer. Because to solve AI problems, a new architecture, which does not yet exist, or an FPGA may be required. At the same time, we had both discrete and computational mathematics in full.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question