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

Support virtual options.match #8749

Closed
Manjiz opened this issue Mar 31, 2020 · 3 comments
Closed

Support virtual options.match #8749

Manjiz opened this issue Mar 31, 2020 · 3 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@Manjiz
Copy link

Manjiz commented Mar 31, 2020

Do you want to request a feature or report a bug?

BUG

What is the current behavior?

Schema.prototype.virtual

options.options.match or options.match not working.

Workaround: add match to populate options.

If the current behavior is a bug, please provide the steps to reproduce.

const projectSchema = new Schema({
  group: { type: Schema.Types.ObjectId, ref: 'Group' }
})
mongoose.model('Project', projectSchema, 'Project')

const groupSchema = new Schema({
})
groupSchema.virtual('projects', {
  ref: 'Project',
  localField: '_id',
  foreignField: 'group',
  justOne: false,
  // @todo not working
  options: {
    match: {
      status: { $ne: '-1' }
    }
  }
})
const groupModel = mongoose.model('Group', groupSchema, 'Group')

groupModel.findOne().populate({ path: 'projects' })

What is the expected behavior?

Exclude some project in projects.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

NodeJS12.12.0 Mongoose5.9.7

@vkarpov15 vkarpov15 added this to the 5.9.9 milestone Apr 2, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Apr 2, 2020
@vkarpov15
Copy link
Collaborator

I'm unable to repro this issue, the below script demonstrates that match filters out projects whose status is equal to the string '-1':

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);
mongoose.set('debug', true);

const { Schema } = mongoose;

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 projectSchema = new Schema({
    group: { type: Schema.Types.ObjectId, ref: 'Group' },
    status: String
  });
  const Project = mongoose.model('Project', projectSchema, 'Project');

  const groupSchema = new Schema({})
  groupSchema.virtual('projects', {
    ref: 'Project',
    localField: '_id',
    foreignField: 'group',
    justOne: false,
    // @todo not working
    options: {
      match: {
        status: { $ne: '-1' }
      }
    }
  });
  const groupModel = mongoose.model('Group', groupSchema, 'Group');

  const g = await groupModel.create({});
  await Project.create({ status: 'foo', group: g });
  await Project.create({ status: '-1', group: g });

  const doc = await groupModel.findOne().populate({ path: 'projects' });
  console.log(doc.toObject({ virtuals: true }));
}

Output:

$ node gh-8749.js 
Mongoose: Group.insertOne({ _id: ObjectId("5e8c7e98be197c2ee66ba5f9"), __v: 0 }, { session: null })
Mongoose: Project.insertOne({ _id: ObjectId("5e8c7e98be197c2ee66ba5fa"), status: 'foo', group: ObjectId("5e8c7e98be197c2ee66ba5f9"), __v: 0 }, { session: null })
Mongoose: Project.insertOne({ _id: ObjectId("5e8c7e99be197c2ee66ba5fb"), status: '-1', group: ObjectId("5e8c7e98be197c2ee66ba5f9"), __v: 0 }, { session: null })
Mongoose: Group.findOne({}, { projection: {} })
Mongoose: Project.find({ status: { '$ne': '-1' }, group: { '$in': [ ObjectId("5e8c7e98be197c2ee66ba5f9") ] } }, { skip: undefined, limit: undefined, perDocumentLimit: undefined, match: { status: { '$ne': '-1' } }, projection: {} })
{ _id: 5e8c7e98be197c2ee66ba5f9,
  __v: 0,
  projects:
   [ { _id: 5e8c7e98be197c2ee66ba5fa,
       status: 'foo',
       group: 5e8c7e98be197c2ee66ba5f9,
       __v: 0,
       id: '5e8c7e98be197c2ee66ba5fa' } ],
  id: '5e8c7e98be197c2ee66ba5f9' }
^C

Please modify the above script to demonstrate the issue you're seeing.

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Apr 7, 2020
@vkarpov15 vkarpov15 removed this from the 5.9.9 milestone Apr 7, 2020
@jonasbetareader
Copy link

I'm using typescript and getting a type unassignable error when trying to use options.match:

userSchema.virtual('bookCount', {
  ref: 'Book',
  localField: 'book',
  foreignField: '_id',
  options: {
    match: {
      listed: true,
    }
  },
  count: true
});

gives me:

Type '{ match: { listed: true; }; }' is not assignable to type 'QueryOptions'.
  Object literal may only specify known properties, and 'match' does not exist in type 'QueryOptions'.ts(2322)

If match should work in virtuals, is there something missing in the typescript declarations?

@vkarpov15 vkarpov15 reopened this Aug 23, 2021
@vkarpov15 vkarpov15 added this to the 5.13.8 milestone Aug 23, 2021
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Aug 23, 2021
@vkarpov15 vkarpov15 changed the title Schema.prototype.virtual match option not working. Support virtual options.match Aug 23, 2021
@vkarpov15
Copy link
Collaborator

Fixed by 5c0140c, I typo-ed the issue number in the commit message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants