试图理解使用populate方法 [英] Trying to understand the use of the populate method
问题描述
我看到我们使用populate的一种方法是将来自另一个集合的一个文档放入父集合中。我刚刚通过这个问题,我希望有人能解释答案给我更好。并告诉我一个实际的使用。下面是一个答案的例子。
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'
}
}));
将数据插入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 ....');
});
}
});
查询
Like.findOne({})。populate('user_id')。exec(function(err,doc){
if(err)
console.log(err);
else
console.log(doc);
});
结果是
{_id:'23456789',
__t:'Like',
user_id:{_id:'1234567',__t:'User',t:'t1' :'test',__v:0},
s:'s1',
__v:0}
b $ b
QUESTION
-
__ t:'User'
从? - 我想是使用populate()或ref来分隔集合,但它看起来像在集合中有用户的文档。我想我想使用填充,所以我可以使文档更小。
3.如果有人真的想帮助解释这个给我,我有一个例子,我一直在努力,我不知道如果我应该使用填充,但如果我应该这将是巨大的,如果你显示我怎么样。
您有
-
-
每个医生可能有1000名医生和很多病人。并且信息将关于他们的实践(例如他们有多少员工)。所以我觉得应该分开关注(一个原因是防止单个文档为病人变大)。所以如果我们要使用populate方法如果你能解释如何设置它为这种情况。我想我可以有一个医生作为父母和一个孩子反对患者和另一个孩子参考实践的信息。所以也许应该有一个对象的objectId数组和一个数组的其他信息
解决方案
Q1:__t:用户来自哪里?
请参阅链接。
mongoose模式继承和打破mongoose-schema-extend的discriminatorKey功能。 mongoose现在将discriminatorKey模式选项设置为
__ t
默认
$ b $ Q2:我想用populate()或ref来分隔集合,但是它看起来像在集合中有用户在其中记录的集合。
似乎你误解了
人口
。 MongoDB中没有连接,但有时我们仍然希望引用其他集合中的文档。这是人口进入的地方。人口是将文档中的指定路径自动替换为来自其他集合的文档的过程。因此populate
不会用于减小文档。
Q3:医生,患者,实践
模式可以如下:
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'}
});
对于模式,很难确定哪个模式更好或者更好它)。我们应该考虑的第一件事是满足我们的查询需求,使查询容易。 mongoDB的设计使查询更有效率。所以我们的模式应该满足它。
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
- where does
__t: 'User'
come from? - 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
- doctors
- patients
- 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
解决方案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). Sopopulate
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.
这篇关于试图理解使用populate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- where does