New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populating different properties WITH different Models in a Mongoose Map Type Schema #10584
Labels
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Comments
@usernamedoesntexist please open a new issue |
IslandRhythms
added
the
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
label
Aug 19, 2021
@silvergravel you'd need to set a 'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
await mongoose.connection.dropDatabase();
const Person = mongoose.model('Person', Schema({ name: String }));
const Book = mongoose.model('Book', Schema({ title: String }));
const schema = new Schema({
myMap: {
type: Map,
of: {
modelId: String,
data: [{
_id: {
type: mongoose.ObjectId,
ref: doc => doc.parent().modelId // <-- use `modelId` as the name of the model used to populate this subdoc
}
}]
}
}
});
const Test = mongoose.model('Test', schema);
const people = await Person.create({ name: 'John' });
const book = await Book.create({ title: 'Intro to CS' });
await Test.create({
myMap: {
key1: { modelId: 'Person', data: [{ _id: people._id }] },
key2: { modelId: 'Book', data: [{ _id: book._id }] }
}
});
const res = await Test.findOne().populate('myMap.$*.data._id');
console.log(require('util').inspect(res.myMap.values(), { depth: 10, colors: true }));
} Does this help? |
@vkarpov15 thank you! This should do it for me i guess. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Do you want to request a feature or report a bug?
Feature Request. I guess.
What is the current behavior?
Okay so there seems to be very little about populating Mongoose Map type schema except for the $* selector, but my use case is slightly different:
So I have a schema :
Now, the data captured via this schema would look something like:
Now the problem is:
I need to populate the
id
s in the map propertycollectionId_1
with thecollectionId_1
model & I need to populate theid
s in the map propertycollectionId_2
with thecollectionId_2
modelSo now, what I tried:
this works, but only populates the
_id
s inside ofcollectionId_1
.I also tried this:
doesn't work at all.
I also tried populating by defining a path explicitly: path:
taggedResources.collectionId_1.data._id
doesn't work.
Can anyone help me with this? Is there even a way? Or will I just have to change my data model back to an array or object.
The reason why I didn't want to use a Object without any clear property definition was because, I wanted to have a more defined 'shape' to the data and stricter type checking. That was the only reason why I thought I should use Map...
Anyway, please help! This issue with Map Schema Type has been bothering me for a while now.
Nodejs version : 12
mongoose version : 5.10.2
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: