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

Slice does not project "select: false" related properties #11940

Closed
2 tasks done
lyaici opened this issue Jun 16, 2022 · 1 comment
Closed
2 tasks done

Slice does not project "select: false" related properties #11940

lyaici opened this issue Jun 16, 2022 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lyaici
Copy link

lyaici commented Jun 16, 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.3.4

Node.js version

16.14.2

MongoDB server version

5.0.6

Description

When using slice on a select: false property defined in the schema, the property is not returned, and using select afterwards on this property only select('property') overwrites the slice (this behaviour is explained on the issue linked below).

So there's currently no way of doing that, except adding this property in the select select('+property') which does not overwrite the slice. Another solution is basically to remove select: false from schema.

This is related to this issue I found here.

Steps to Reproduce

const Post = new Schema({
  ...
  comments: { type: [Comment], select: false }
});

PostModel.find(...).slice('comments', [limit, offset]).exec()
// This doesn't return comments

PostModel.find(...).slice('comments', [limit, offset]).select('comments').exec()
//This doesn't apply slice (overwritten by the select)

PostModel.find(...).slice('comments', [limit, offset]).select('+comments').exec()
//This works but every other properties are selected (except select: false ones that are not included in the select)

Expected Behavior

const Post = new Schema({
  ...
  comments: { type: [Comment], select: false }
});

PostModel.find(...).slice('comments', [limit, offset]).exec()
// This to return comments only

Or

PostModel.find(...).slice('comments', [limit, offset]).select('comments').exec()
// This to apply the slice and to return comments only
@lyaici lyaici changed the title Slice do not project "select: false" concerned properties Slice do not project "select: false" related properties Jun 16, 2022
@lyaici lyaici changed the title Slice do not project "select: false" related properties Slice does not project "select: false" related properties Jun 16, 2022
@vkarpov15 vkarpov15 added this to the 6.3.10 milestone Jun 17, 2022
@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 Jun 17, 2022
@IslandRhythms IslandRhythms 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 Jun 21, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const commentSchema = new mongoose.Schema({
    comment: String
});

const testSchema = new mongoose.Schema({
    name: String,
    comments: { type: [commentSchema], select: false}
});

const Test = mongoose.model('Test', testSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    await Test.create({
        name: 'Test',
        comments: [{comment: 'This is a test'}, {comment: 'This should show up'}]
    });

    const fail = await Test.find().slice('comments', [{limit: 1}, {offset: 0}]);
    console.log('fail', fail);

    const alsoFail = await Test.find().slice('comments', [{limit: 1}, {offset: 0}]).select('comments');

    console.log('alsoFail', alsoFail);
    console.log(alsoFail[0].comments)
    const works = await Test.find().slice('comments', [{limit: 1}, {offset: 0}]).select('+comments');

    console.log('returns everything', works);
}

run();

output:

fail [
  {
    _id: new ObjectId("62b207feb575a66a7934eccb"),
    name: 'Test',
    __v: 0
  }
]
alsoFail [
  {
    _id: new ObjectId("62b207feb575a66a7934eccb"),
    comments: [ [Object], [Object] ]
  }
]
[
  {
    comment: 'This is a test',
    _id: new ObjectId("62b207feb575a66a7934eccc")
  },
  {
    comment: 'This should show up',
    _id: new ObjectId("62b207feb575a66a7934eccd")
  }
]
C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:210
                    callback(new error_1.MongoServerError(document));
                             ^

MongoServerError: First argument to $slice must be an array, but is of type: object
    at Connection.onMessage (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:210:30)
    at MessageStream.<anonymous> (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:63:60)
    at MessageStream.emit (node:events:390:28)
    at processIncomingData (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\node_modules\mongodb\lib\cmap\message_stream.js:132:20)
    at MessageStream._write (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:390:28) {
  ok: 0,
  code: 28724,
  codeName: 'Location28724',
  [Symbol(errorLabels)]: Set(0) {}
}

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

3 participants