如何在mongoose中填充数据?

huangapple go评论53阅读模式
英文:

how to populate in mongoose?

问题

var headSchema = new Schema({
  typename: String,
  headname: String,
  status: { type: Number, default: 1 }
});
var Head = mongoose.model("heads", headSchema);

var mgmtSchema = new Schema({
  headtype: String,
  type: { type: Schema.Types.ObjectId, ref: "Head" },
  datefrom: Date,
  dateto: Date,
  title: String,
  amount: Number,
  description: String,
  created_at: { type: Date, default: Date.now },
  updated_at: { type: Date, default: Date.now },
  status: Number
});

var HeadMgmt = mongoose.model("headmgmt", mgmtSchema);

HeadMgmt.find({})
  .populate({ path: "type", select: "headname" })
  .exec(function(err, result) {
    console.log(result);
  });
英文:
var headSchema = new Schema({
  typename: String,
  headname: String,
  status: { type: Number, default: 1 }
});
var Head = mongoose.model("heads", headSchema);

var mgmtSchema = new Schema({
  headtype: String,
  type: { type: Schema.Types.ObjectId, ref: "Head" },
  datefrom: Date,
  dateto: Date,
  title: String,
  amount: Number,
  description: String,
  created_at: { type: Date, default: Date.now },
  updated_at: { type: Date, default: Date.now },
  status: Number
});

var HeadMgmt = mongoose.model("headmgmt", mgmtSchema);

HeadMgmt.find({})
  .populate({ path: "type", select: "headname" })
  .exec(function(err, result) {
    console.log(result);
  });

I have two schemas, also foreign key reference. The second schema has some value. I need to display the lists in one html table. How can print headname from head schema?

答案1

得分: 0

mgmtSchema中,您需要将类型字段中的ref值从Head更改为heads,因为您在设置Head模型时使用了heads,如下所示:

var Head = mongoose.model("heads", headSchema);

所以您的mgmtSchema必须像这样:

var mgmtSchema = new Schema({
  headtype: String,
  type: { type: Schema.Types.ObjectId, ref: "heads" },
  datefrom: Date,
  dateto: Date,
  title: String,
  amount: Number,
  description: String,
  created_at: { type: Date, default: Date.now },
  updated_at: { type: Date, default: Date.now },
  status: Number
});

您当前的populate代码的结果将类似于:

[
    {
        "_id": "5e13483dd62cd92e3c3e8dcd",
        "headtype": "headtype",
        "type": {
            "_id": "5e1347671b0bd339e0521139",
            "headname": "headname 1"
        },
        "title": "title",
        "amount": 123,
        "description": "description",
        "created_at": "2020-01-06T14:46:21.906Z",
        "updated_at": "2020-01-06T14:46:21.906Z",
        "__v": 0
    }
]
英文:

In mgmtSchema you need to change the ref value in type field from Head to heads, because you used headssetup your Head model like this:

var Head = mongoose.model("heads", headSchema);

So your mgmtSchema must be like this:

var mgmtSchema = new Schema({
  headtype: String,
  type: { type: Schema.Types.ObjectId, ref: "heads" },
  datefrom: Date,
  dateto: Date,
  title: String,
  amount: Number,
  description: String,
  created_at: { type: Date, default: Date.now },
  updated_at: { type: Date, default: Date.now },
  status: Number
});

The result of your current populate code will be something like this:

[
    {
        "_id": "5e13483dd62cd92e3c3e8dcd",
        "headtype": "headtype",
        "type": {
            "_id": "5e1347671b0bd339e0521139",
            "headname": "headname 1"
        },
        "title": "title",
        "amount": 123,
        "description": "description",
        "created_at": "2020-01-06T14:46:21.906Z",
        "updated_at": "2020-01-06T14:46:21.906Z",
        "__v": 0
    }
]

huangapple
  • 本文由 发表于 2020年1月6日 16:37:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定