-
basically my question is "are reference maps supported" and when yes, is this the right way or does the // NodeJS: 16.1.0
// MongoDB: 4.2-bionic (Docker)
import * as mongoose from "mongoose"; // mongoose@5.12.10
import { inspect } from "util";
const DummySchema = new mongoose.Schema({
dummy: String
});
const TestSchema = new mongoose.Schema({
testprop: {
type: Map,
of: mongoose.Schema.Types.ObjectId,
ref: "dummy"
}
});
const DummyModel = mongoose.model("dummy", DummySchema);
const TestModel = mongoose.model("test", TestSchema);
(async () => {
await mongoose.connect(`mongodb://localhost:27017/`, { useNewUrlParser: true, dbName: "verifyMASTER", useCreateIndex: true, useUnifiedTopology: true });
console.log("schema path", TestModel.schema.path("testprop"));
const dummy_doc1 = await DummyModel.create({ dummy: "hello1" });
const dummy_doc2 = await DummyModel.create({ dummy: "hello2" });
const test_doc1 = await TestModel.create({ testprop: [["hello1", dummy_doc1], ["hello2", dummy_doc2]] });
const found_doc1 = await TestModel.findById(test_doc1).orFail().exec();
console.log("found", inspect(found_doc1, false, 5));
await found_doc1.populate("testprop.$*");
console.log("populated", inspect(found_doc1, false, 5));
await mongoose.disconnect();
})(); PS: thanks to #9359 i already know that it is supported to have references inside maps with an extra property, but my question is when the ObjectId is directly the type of the map |
Beta Was this translation helpful? Give feedback.
Answered by
vkarpov15
Jun 4, 2021
Replies: 1 comment 6 replies
-
At first glance this code looks right. Are you getting an error or some sort of unexpected behavior? |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
hasezoey
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At first glance this code looks right. Are you getting an error or some sort of unexpected behavior?