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

[Symbol(mongoose#trustedSymbol)]: true when there is match and localField is different from _id in populate #15263

Closed
2 tasks done
MikeKoval opened this issue Feb 18, 2025 · 1 comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Milestone

Comments

@MikeKoval
Copy link

Prerequisites

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

Mongoose version

7.8.2

Node.js version

22.x

MongoDB server version

6.x

Typescript version (if applicable)

No response

Description

Hi!

I am bumping into issue like this #12834 .
That issue was related to _id. This time i have same issue because i dont use _id as localField in virtual field declaration.

I am getting

Mongoose: mlaGroups.find({ status: 'active', key: { '$in': [ ObjectId("67b4311e6bac0a9f51d75792") ], [Symbol(mongoose#trustedSymbol)]: true }}, { skip: undefined, limit: undefined, perDocumentLimit: undefined, projection: { name: 1, key: 1 }})

in console when add match in populate or in virtual field declaration.

Here is how my virtual field declaration looks like:

foreignField: 'key',
    localField: 'groupKeys',
    match: {
      status: GroupStatus.ACTIVE,
    },
    justOne: false,

Here is how i tried to call it:

 Flow
        .find(filter, projection)
        .populate({
            path: 'groups',
            select: 'name',
            // match: {
                // status: 'active', // if remove match from schema and uncomment this, it will be not working also
            // },
        })

So i have a question, why not to use value from localField instead of hardcoded _id?

Steps to Reproduce

  1. Use localField different from _id
  2. Add match either in virtual field declaration or populate
  3. Cannot get data from db because i am getting magic [Symbol(mongoose#trustedSymbol)]: true in the query...

Expected Behavior

  1. To be able to use combination of localField different from _id and match either in populate call or in virtual schema declaration
@vkarpov15 vkarpov15 added this to the 8.10.2 milestone Feb 18, 2025
@vkarpov15
Copy link
Collaborator

I'm unable to repro, following script works fine:

const mongoose = require('mongoose');
const { Schema, model, Types } = mongoose;

mongoose.connect('mongodb://localhost:27017/test');
mongoose.set('debug', true);

const GroupSchema = new Schema({
  name: String,
  key: { type: Types.ObjectId, required: true, unique: true },
  status: { type: String, enum: ['active', 'inactive'], required: true },
});
const Group = model('Group', GroupSchema);

const FlowSchema = new Schema({
  name: String, 
  groupKeys: [{ type: Types.ObjectId, ref: 'Group' }]
});

FlowSchema.virtual('groups', {
  ref: 'Group',
  localField: 'groupKeys', // Not _id
  foreignField: 'key',
  //match: { status: 'active' },
  justOne: false,
});

const Flow = model('Flow', FlowSchema);

(async function run() {
    await mongoose.connection.dropDatabase();

    const group1 = await Group.create({ name: 'Group 1', key: new Types.ObjectId(), status: 'active' });
    const group2 = await Group.create({ name: 'Group 2', key: new Types.ObjectId(), status: 'inactive' });
    const flow = await Flow.create({ name: 'Flow 1', groupKeys: [group1.key, group2.key] });

    const result = await Flow.findOne({ _id: flow._id }).populate({
      path: 'groups',
    });
    console.log(result.toObject({ virtuals: true }));
})();

Output shows groups virtual is populated with both docs

{
  _id: new ObjectId('67b510f204b07e862773016c'),
  name: 'Flow 1',
  groupKeys: [
    new ObjectId('67b510f204b07e8627730165'),
    new ObjectId('67b510f204b07e8627730168')
  ],
  __v: 0,
  groups: [
    {
      _id: new ObjectId('67b510f204b07e8627730166'),
      name: 'Group 1',
      key: new ObjectId('67b510f204b07e8627730165'),
      status: 'active',
      __v: 0,
      id: '67b510f204b07e8627730166'
    },
    {
      _id: new ObjectId('67b510f204b07e8627730169'),
      name: 'Group 2',
      key: new ObjectId('67b510f204b07e8627730168'),
      status: 'inactive',
      __v: 0,
      id: '67b510f204b07e8627730169'
    }
  ],
  id: '67b510f204b07e862773016c'
}

[Symbol(mongoose#trustedSymbol)]: is a red herring here, that is just an internal marker that Mongoose uses for sanitizeFilter, symbols in query filter aren't actually sent to MongoDB.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Feb 18, 2025
vkarpov15 added a commit that referenced this issue Feb 23, 2025
fix(debug): avoid printing trusted symbol in debug output
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
Projects
None yet
Development

No branches or pull requests

2 participants