Skip to content
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

[Question] Populate a Map #9359

Closed
InsOpDe opened this issue Aug 26, 2020 · 7 comments
Closed

[Question] Populate a Map #9359

InsOpDe opened this issue Aug 26, 2020 · 7 comments
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Milestone

Comments

@InsOpDe
Copy link

InsOpDe commented Aug 26, 2020

Question
I have schema type Map in my mongoose model. In this map, each element has reference to another model. I know that it's possible to populate attributes in array, but how about Map type? Be cause nesting like "map_type_attribute.some_attribute_to_populate" doesn't work. :)

This is my model:

const Mongoose = require('mongoose');

const parameter = Mongoose.Schema({
  definition: {
    type: Mongoose.Schema.Types.ObjectId,
    ref:  'Definition',
  },
  value:      {},
}, {_id: false});

const schema = Mongoose.Schema({
  model:      {
    type: Mongoose.Schema.Types.ObjectId,
    ref:  'Model'
  },
  name:       String,
  objectid:   Number,
  externalId: String,
  properties: Mongoose.Schema.Types.Mixed,
  parameters: {
    type: Map,
    of:   parameter
  }
});

module.exports = Mongoose.model('Element', schema);

This is how i'm trying to populate definition field:

 const request = Element.find(query, projection);
  request.populate('parameters.definition');
  request.exec( (err, docs) => {
...

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: v13.13.0
mongoose: 5.9.26
mongodb (docker): bitnami/mongodb:4.2.8-debian-10-r47

@vkarpov15 vkarpov15 added this to the 5.10.2 milestone Aug 28, 2020
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Aug 28, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.10.2, 5.10.3 Aug 28, 2020
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Aug 30, 2020
@vkarpov15
Copy link
Collaborator

Confirmed, here's the repro script:

'use strict';

const mongoose = require('mongoose');

const { Schema } = mongoose;

mongoose.set('debug', true);

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

const parameter = Schema({
  definition: {
    type: mongoose.Schema.Types.ObjectId,
    ref:  'Definition',
  },
  value:      {},
}, {_id: false});

const schema = Schema({
  model:      {
    type: mongoose.Schema.Types.ObjectId,
    ref:  'Model'
  },
  name:       String,
  objectid:   Number,
  externalId: String,
  properties: mongoose.Schema.Types.Mixed,
  parameters: {
    type: Map,
    of:   parameter
  }
});

  const Definition = mongoose.model('Definition', Schema({ text: String }));
  const Element = mongoose.model('Element', schema);

  const def = await Definition.create({ text: 'test' });
  await Element.create({ parameters: { test: { definition: def } } });

  const doc = await Element.findOne().populate('parameters.$*.definition');
  console.log(doc.parameters);
}

vkarpov15 added a commit that referenced this issue Aug 30, 2020
@vkarpov15
Copy link
Collaborator

Thanks for reporting, fix will be in 5.10.3. You'll have to do Element.findOne().populate('parameters.$*.definition');, note the $* part.

@ManimuthurajT
Copy link

ManimuthurajT commented Nov 2, 2021

@vkarpov15

I have schema type Map in my mongoose model. How to use refPath in Map?using "values.$*.refp" doesn't work.
This is my model:

`const fieldValuesSchema = new Schema({
fieldId: { type: Schema.Types.ObjectId, required: true },
valueObject: {
type: Schema.Types.ObjectId,
refPath: "values.$*.refp",
},
modelName: {
type: Schema.Types.String,
},
});

const rowSchema =new Schema ({
listId: { type: Schema.Types.ObjectId, required: true },
values: { type: Schema.Types.Map, of: listFieldValuesSchema },
});
`

@vkarpov15
Copy link
Collaborator

@ManimuthurajT where is refp and listFieldValuesSchema defined?

@ManimuthurajT
Copy link

Thanks for your response here is the definition of refp and listFieldValuesSchema defined

const listFieldValuesSchemaDef = {
    ...
    value: {
        type: Schema.Types.Mixed,
    },
    valueObject: {
        type: Schema.Types.ObjectId,
        refPath: "values.$*.refp",
    },
    refp: {
        type: Schema.Types.String,
    },
};

const listRowSchemaDef = {
    ...
    values: { type: Schema.Types.Map, of: listFieldValuesSchema },
};

@vkarpov15 vkarpov15 reopened this Dec 12, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.10.3, 6.1.4 Dec 12, 2021
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Dec 12, 2021
@ManimuthurajT
Copy link

ManimuthurajT commented Dec 21, 2021

@vkarpov15 repro script


const mongoose = require("mongoose");

const { Schema } = mongoose;

mongoose.set("debug", true);

run().catch((err) => console.log(err));

async function run() {
  await mongoose.connect("mongodb://localhost:27017/test", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  await mongoose.connection.dropDatabase();

  // user schema
  const userSchema = Schema({
    name: { type: mongoose.Schema.Types.String, required: true },
  });

  // list schema
  const listSchema = Schema({
    listName: { type: mongoose.Schema.Types.String, required: true },
  });

  // row value schema
  const rowValuesSchema = Schema({
    valueObject: {
      type: mongoose.Schema.Types.ObjectId,
      refPath: "values.$*.refp",
    },
    refp: {
      type: mongoose.Schema.Types.String,
    },
  });

  // row schema
  const rowSchema = Schema({
    sortOrder: { type: mongoose.Schema.Types.Number, required: true },
    values: { type: mongoose.Schema.Types.Map, of: rowValuesSchema },
  });

  const User = mongoose.model("User", userSchema);
  const List = mongoose.model("List", listSchema);
  const Row = mongoose.model("Row", rowSchema);

  const createUser = await User.create({ name: "test" });
  const createList = await List.create({ listName: "hi" });

  await Row.create({
    sortOrder: 1,
    values: {
      [createList._id]: {
        valueObject: mongoose.Types.ObjectId(createUser._id),
        refp: "User",
      },
    },
  });

  const row = await Row.findOne().populate({
    path: "values",
    populate: { path: "valueObject" },
  });
  console.log("row", row);
}```

@vkarpov15
Copy link
Collaborator

@ManimuthurajT here's what you do what you're trying to do:

  const row = await Row.findOne().populate({
    path: "values.$*.valueObject" // <-- no need for deep populate since `values` is already a property in row
  });

There's a bug that prevents refPath from working with maps of subdocs, we're fixing that for v6.1.4. As a workaround, you can do:

  // row value schema
  const rowValuesSchema = Schema({
    valueObject: {
      type: mongoose.Schema.Types.ObjectId,
      ref: subdoc => subdoc.refp // <-- use `ref` function syntax rather than `refPath`
    },
    refp: {
      type: mongoose.Schema.Types.String,
    },
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Projects
None yet
Development

No branches or pull requests

3 participants