Sai*_*Ram 8 mongoose mongoose-populate
我遇到了以下我不明白的代码行,尽管有很多教程提供了与示例相关的信息,populate但是没有任何教程可以解释其含义。这里有一个示例
var mongoose = require('mongoose'), Schema = mongoose.Schema
var PersonSchema = new Schema({
name : String,
age : Number,
stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : {
type: Schema.ObjectId,
ref: 'Person'
},
title : String,
fans : [{ type: Schema.ObjectId, ref: 'Person' }]
});
var Story = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
Story.findOne({ title: /Nintendo/i }).populate('_creator') .exec(function (err, story) {
if (err) ..
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
})
Run Code Online (Sandbox Code Playgroud)
小智 20
我随机遇到了这个问题,但是我觉得我需要在这里提供帮助,即使它已经过时了,因为我不相信它的解释方式:
Populate()函数填充...
对于以英语为母语的人来说,这可能很清楚,但对于其他人则可能不是。
填充将自动用其他集合中的文档替换文档中的指定路径
让我们举个例子
Story.findOne({ title: Nintendo })
Run Code Online (Sandbox Code Playgroud)
将返回一个这样的故事:
{
_creator : A0jfdSMmEJj*9, //id of the creator (totaly random, just for a clear example)
title : Nintendo,
fans : [r432i900fds09809n, fdsjifdsjfueu88] // again, totaly random that i've typed here
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,这样的请求就足够了,因为我们不在乎作者或粉丝,因此拥有一些ID不会对我们造成太大影响。
但是,在需要该作者的情况下,我将需要再次发出请求以在数据库中找到它。除此以外,在猫鼬中,我们有一个名为populate()的聪明函数,我们可以将其链接到先前的请求,以便直接在我们的答案中获取该信息,而无需明确执行其他请求。
Story.findOne({ title: Nintendo }).populate('_creator')
Run Code Online (Sandbox Code Playgroud)
将返回
{
_creator : {
_id : A0jfdSMmEJj*9,
name: Sai,
age: 100,
stories : [fdsfdsfdsew38u, 89hr3232, ...]
},
title : Nintendo,
fans : [r432i900fds09809n, fdsjifdsjfueu88]
}
}
Run Code Online (Sandbox Code Playgroud)
但是也许那是太多的信息,我们不希望他写的故事以及他的年龄和名字足够。然后可以填充另一个包含我们需要的字段的参数
Story.findOne({ title: Nintendo }).populate('_creator', 'name age')
Run Code Online (Sandbox Code Playgroud)
结果==>
{
_creator : {
name: Sai,
age: 100,
},
title : Nintendo,
fans : [r432i900fds09809n, fdsjifdsjfueu88]
}
}
Run Code Online (Sandbox Code Playgroud)
Akh*_*l P 19
populate()mongoose 中的函数用于填充引用中的数据。在您的示例StorySchema中,有一个_creator字段,该_id字段将引用基本上ObjectId是 mongodb 文档的字段。
populate() 函数可以接受一个字符串或一个对象作为输入。
其中 string 是需要填充的字段名称。在你的情况下是_creator。在 mongoose 从 mongodb 中找到一个文档后,结果如下
_creator: {
name: "SomeName",
age: SomeNumber,
stories: [Set Of ObjectIDs of documents in stories collection in mongodb]
},
title: "SomeTitle",
fans: [Set of ObjectIDs of documents in persons collection in mongodb]
Run Code Online (Sandbox Code Playgroud)
populate 也可以接受对象作为输入。
你可以在populate()这里找到 mongoose函数的文档:http :
//mongoosejs.com/docs/2.7.x/docs/populate.html或https://mongoosejs.com/docs/populate.html
如果您需要跨多个级别进行填充,则此问题的答案并不完整。我们可以用它来获取用户的好友列表。但是要检索用户朋友的朋友,我们可以这样做:
const userSchema = new Schema({
name: String,
friends: [{ type: ObjectId, ref: 'User' }]
});
User.
findOne({ name: 'Val' }).
populate({
path: 'friends',
// Get friends of friends - populate the 'friends' array for every friend
populate: { path: 'friends' }
});
Run Code Online (Sandbox Code Playgroud)
.populate() 被用来只带来需要的信息。
没有 .populate() 的示例
User.findOne({ name: Bob })
Run Code Online (Sandbox Code Playgroud)
将返回
{
Bob : {
_id : dasd348ew,
email: bob@example.com,
age: 25,
job: teacher,
nationality: American,
}
}
Run Code Online (Sandbox Code Playgroud)
以 .populate() 为例
User.findOne({ name: Bob }).populate("Bob", "job email")
Run Code Online (Sandbox Code Playgroud)
将返回
{
Bob : {
job: teacher,
email: bob@example.com,
}
}
Run Code Online (Sandbox Code Playgroud)