Node JS:填充自动增量字段(猫鼬)

问题描述 投票:0回答:1

我有两个集合,第一个是带有自动递增字段的,

我在第二个集合中引用了自动增量字段,但是带有填充函数的查找不会返回填充结果。

Table1

const mongoose = require("mongoose");
var autoIncrement = require("mongoose-auto-increment");

const table1Schema = mongoose.Schema({
  name: String,
  displayed: { type: Boolean, default: true },
  updatedAt: Date,
  createdAt: Date
});

autoIncrement.initialize(mongoose.connection);
table1Schema.plugin(autoIncrement.plugin, { model: "table1", startAt: 1 });

module.exports = mongoose.model("table1", table1Schema);

table2

const table2Schema = mongoose.Schema({
  categoryId: { type: Number, ref: "table1" },
  displayed: { type: Boolean, default: true }
});

module.exports = mongoose.model("table2", table2Schema);

查询:

var table2_schema = require("../schemas/table2_schema.js");

module.exports.findPopulateFunction = function() {
  table2_schema
    .find({})
    .populate("categoryId")
    .exec(function(err, doc) {
      console.log("err : ", err);
      console.log("docxx : ", doc);
    });
};
node.js mongoose auto-increment populate
1个回答
0
投票

问题是我正在使用脚本来插入“ _id”字段号,我删除了自动增量的声明,它可以成功运行

© www.soinside.com 2019 - 2024. All rights reserved.