在猫鼬中填充是什么意思? [英] what does populate in mongoose mean?

查看:20
本文介绍了在猫鼬中填充是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下我无法理解的代码行,尽管有很多教程提供了与 populate 示例相关的信息,但没有一个能解释它的确切含义.这是一个例子

var mongoose = require('mongoose'), Schema = mongoose.Schemavar PersonSchema = new Schema({名称:字符串,年龄:数字,故事:[{ 类型:Schema.ObjectId,参考:'故事'}]});var StorySchema = 新架构({_创作者:{类型:Schema.ObjectId,参考:'人'},标题:字符串,粉丝:[{ 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) {如果(错误)..console.log('创建者是 %s', story._creator.name);//打印创建者是 Aaron"})

解决方案

populate() mongoose 中的函数用于填充引用中的数据.在您的示例中,StorySchema 具有 _creator 字段,该字段将引用 _id 字段,该字段基本上是mongodb 文档.

<块引用>

populate() 函数可以接受一个字符串或一个对象作为输入.

其中 string 是需要填充的字段名称.在您的情况下,它是 _creator.在 mongoose 从 mongodb 中找到一个文档后,结果如下

_creator: {name: "SomeName",年龄:SomeNumber,故事:[mongodb 故事集合中文档的 ObjectID 集]},title: "SomeTitle",粉丝:[mongodb 中人员集合中文档的 ObjectID 集]

<块引用>

populate 也可以接受对象作为输入.

你可以在这里找到 mongoose 的 populate() 函数的文档:http://mongoosejs.com/docs/2.7.x/docs/populate.htmlhttps://mongoosejs.com/docs/populate.html

I came across the following line of code which I couldn't understand ,although there are lot of tutorials that gives information related to examples of populate but there is none that explains what exactly it means.Here is a example

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"
})

解决方案

populate() function in mongoose is used for populating the data inside the reference. In your example StorySchema is having _creator field which will reference to the _id field which is basically the ObjectId of the mongodb document.

populate() function can accept a string or an object as an input.

Where string is the field name which is required to be populated. In your case that is _creator. After mongoose found one doc from mongodb and the result of that is like below

_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]

populate can also accept the object as an input.

You can find the documents of mongoose's populate() function here : http://mongoosejs.com/docs/2.7.x/docs/populate.html or https://mongoosejs.com/docs/populate.html

这篇关于在猫鼬中填充是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆