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

Using populate on schema with combined Map of Array<Schema> will not return Array in v6 #12494

Closed
2 tasks done
lattam opened this issue Oct 1, 2022 · 2 comments
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lattam
Copy link

lattam commented Oct 1, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.3

Node.js version

14.16.1

MongoDB server version

4.4.3

Description

Using populate on schema with "Map of Array" will ignore the Array part and will return only one Schema object after upgrading from mongoose 5 to 6.

Steps to Reproduce

const mongoose = require('mongoose');

const connection = mongoose.createConnection('mongodb://localhost:27037/test');

const User = new mongoose.Schema({
  name: String,
  addresses: {
    type: Map,
    of: [{
      type: mongoose.Schema.Types.ObjectId,
      ref:  'Address',
    }],
    default: {},
  },
});

const Address = new mongoose.Schema({
  city: String,
});

const UserModel = connection.model('User', User);
const AddressModel = connection.model('Address', Address);

async function init() {
  await UserModel.deleteMany({});
  await AddressModel.deleteMany({});

  const address = await new AddressModel({ city: 'London' }).save();

  await new UserModel({
    name: 'Name',
    addresses: {
      home: [address._id]
    }
  }).save();
}

async function test() {
  const query = UserModel.find({});

  query.populate({
    path: 'addresses.home',
  });

  const documents = await query.exec();

  console.log(JSON.stringify(documents));
  // v5.X.X outputs:
  // [{"_id":"63383ac599e8f5243db0cc8f","name":"Name", "addresses":{"home":[{"_id":"63383ac599e8f5243db0cc8d","city":"London"}]}}]

  // v6.X.X outputs:
  // [{"_id":"63383b503110bec5287a2ed7","name":"Name","addresses":{"home":{"_id":"63383b503110bec5287a2ed5","city":"London"}}}]
}

async function run() {
  await init();
  await test();
  connection.close();
}

run();

Expected Behavior

Return the Array of sub elements, not just the first object.

@chinmaykumbhare
Copy link

hi @lattam can you assign me this issue? I can fix it.

@hasezoey hasezoey added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Oct 2, 2022
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Oct 10, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const connection = mongoose.createConnection('mongodb://localhost:27017/test');

const User = new mongoose.Schema({
  name: String,
  addresses: {
    type: Map,
    of: [{
      type: mongoose.Schema.Types.ObjectId,
      ref:  'Address',
    }],
    default: {},
  },
});

const Address = new mongoose.Schema({
  city: String,
});

const UserModel = connection.model('User', User);
const AddressModel = connection.model('Address', Address);

async function init() {
  await UserModel.deleteMany({});
  await AddressModel.deleteMany({});

  const address = await new AddressModel({ city: 'London' }).save();

  await new UserModel({
    name: 'Name',
    addresses: {
      home: [address._id]
    }
  }).save();
}

async function test() {
  const query = UserModel.find({});

  query.populate({
    path: 'addresses.home',
  });

  const documents = await query.exec();

  console.log(JSON.stringify(documents));
  // v5.X.X outputs:
  // [{"_id":"63383ac599e8f5243db0cc8f","name":"Name", "addresses":{"home":[{"_id":"63383ac599e8f5243db0cc8d","city":"London"}]}}]

  // v6.X.X outputs:
  // [{"_id":"63383b503110bec5287a2ed7","name":"Name","addresses":{"home":{"_id":"63383b503110bec5287a2ed5","city":"London"}}}]
}

async function run() {
  await init();
  await test();
  connection.close();
}

run();

@vkarpov15 vkarpov15 modified the milestones: 6.6.9, 6.6.8 Oct 24, 2022
vkarpov15 added a commit that referenced this issue Oct 27, 2022
Better support for populating maps of arrays of refs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

5 participants