【问题标题】:Node JS : Populate auto increment field (Mongoose)节点 JS:填充自动增量字段(猫鼬)
【发布时间】:2020-06-25 10:34:45
【问题描述】:

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

我在第二个集合中对自动增量字段进行了引用,但使用填充函数查找并没有返回填充的结果。

表 1

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

【问题讨论】:

  • 猫鼬不支持ObjectId而不是数字
  • 你确定这是一个数字吗?我认为外键与 ObjectId 一起工作(在 mongodb 中)
  • 是的,它是一个数字,我也尝试过虚拟填充但无法正常工作
  • 这个函数怎么调用?它还会返回空的,还是只返回没有填充 table1 的 table2 文档?
  • 它返回没有表1的table2文档

标签: node.js mongoose auto-increment populate


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 2018-10-14
    • 2015-06-20
    • 2021-05-06
    • 2018-12-12
    • 2015-04-06
    相关资源
    最近更新 更多