【发布时间】: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