Trying to understand the use of the populate method

喜你入骨 提交于 2019-12-12 02:39:37

问题


I see that one way we use populate is to put one document from another collection into a "parent" collection. I was just going through this question and I was hoping someone could explain the answer to me better. And show me a practical use. Here is an example from the answer.

var PersonSchema = new mongoose.Schema({
    t: String
}, {collection: 'persons'});

var User = mongoose.model('User', PersonSchema.extend({
  _id: String,
  name: String
}));

var ParentSchema = new mongoose.Schema({
    s: String
}, {collection: 'parent'});

var Like = mongoose.model('Like', ParentSchema.extend({
  _id: String,
  user_id: {
    type: String,
    ref: 'User'
  }
}));

Insert Data into DB,

var user = new User({
    t: 't1',
    _id: '1234567',
    name: 'test'
});

var like = new Like({
    s: 's1',
    _id: '23456789',
});

user.save(function(err, u){
    if(err)
        console.log(err);
    else {
        like.user_id = u._id;
        console.log(like);
        like.save(function(err) {
            if (err)
                console.log(err);
            else
                console.log('save like and user....');
        });
    }
});

Query by

Like.findOne({}).populate('user_id').exec(function(err, doc) {
    if (err)
        console.log(err);
    else
        console.log(doc);
});

And the result is

{ _id: '23456789',
  __t: 'Like',
  user_id: { _id: '1234567', __t: 'User', t: 't1', name: 'test', __v: 0 },
  s: 's1',
  __v: 0 }

QUESTION

  1. where does __t: 'User' come from?
  2. I was thinking that using populate() or ref that would separate the collections but it looks like at the end the like collection has the users document in it. I think I wanted to use populate so I could make a document smaller. 3.Also if someone really wanted to help explain this to me I have an example that I have been trying to do and I don't know if I should use populate but if I should it would be great if you show me how. Here is the example.

You have

  1. doctors
  2. patients
  3. information about the practice

There could be like a 1000 doctors and lots of patients for each doctor. and the information will be about their practice(like how many employees they have). so I feel that there should be a separation of concern.(one reason is to prevent a single document for a patient from getting to big). So If we're going with the populate method If you could explain how to set it up for this case. I guess I could have a doctor as a parent and a child refs for patients and another child refs for information about practice. so maybe there should be an array of objectId for the patients and an array for Other information


回答1:


Q1: where does __t: 'User' come from?

Refer to this link.

mongoose now includes schema inheritance and discriminatorKey functionality that breaks mongoose-schema-extend. mongoose now sets the discriminatorKey schema option to __t by default


Q2: I was thinking that using populate() or ref that would separate the collections but it looks like at the end the like collection has the users document in it. I think I wanted to use populate so I could make a document smaller.

It seems you misunderstand the meaning of Population. There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where population comes in. Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). So populate is not used to make document smaller.


Q3: Doctor, Patient, Practice

Schema could be as following:

var DoctorSchema = new Schema ({
   name: String,
   // ... other field
});

var PatientSchema = new Schema ({
  name: String,
  doctor: {type: Schema.ObjectId,
          ref: 'Doctor'}
});

var PracticeSchema = new Schema ({
  ff: String,
  patientId: {type: Schema.ObjectId,
             ref: 'Patient'},
  doctorId: {type: Schema.ObjectId,
             ref: 'Doctor'}
});

As for schema, it is hard to determine which schema is better or not, (with populate or without it). The first thing we should consider is to meet our query requirement, to make the query easy. The design of mongoDB to make the query more efficiently. So our schema should meet it.



来源:https://stackoverflow.com/questions/35003893/trying-to-understand-the-use-of-the-populate-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!