Hello,
I would like to create a reference between a document of a collection and one embedded subdocument of another collection
For example, this is the first collection
import mongoose from "mongoose";
const deviceSchema = new mongoose.Schema(
{
mac: {
type: String,
required: true,
},
unit: {
type: mongoose.Schema.Types.ObjectId,
ref: "sede", // but the reference should be to sede.room
},
);
const Device = mongoose.model("device", dispoSchema, "device");
export default Dispo;
the second collection is
import mongoose from "mongoose";
const sedeSchema = new mongoose.Schema(
{
codifica: {
type: String,
required: true,
unique: true,
},
room: [
{
code: {
type: String,
},
place: {
type: String,
},
},
],
},
{ timestamps: true }
);
const Sede = mongoose.model("sede", sedeSchema, "sede");
export default Sede;
This is a simplified example document of the first collection
{
_id: ObjectId("abc123")
mac: "abcd11"
unit: ObjectId("123456")
}
This is a simplified document of the second collection
{
_id: ObjectId("xsd444")
codifica: "abceee21"
room: [
{_id: ObjectId("123456"), code: "room1", place: "floor"},
{_id: ObjectId("333333"), code: "room2", place: "ceil"},
]
}
I expect to populate all documents from the first collection to achieve this
[...
{
_id: ObjectId("abc123")
mac: "abcd11"
unit: {
id: ObjectId("123456"),
code: "room1",
place: "floor"
}
},
...]
Thanks!!!