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

Select not working if inside an array for schemas #10029

Closed
Jatin-MYST opened this issue Mar 15, 2021 · 6 comments
Closed

Select not working if inside an array for schemas #10029

Jatin-MYST opened this issue Mar 15, 2021 · 6 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@Jatin-MYST
Copy link

Jatin-MYST commented Mar 15, 2021

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

Possibly a bug where following usage of select doesnt seem to do anything. I still see the shouldntshow field.

shouldntshow: [{ // <------ it shouldnt show because select is false
  type: String,
  select: false
}],

If intended then please let me know the correct usage.

Note that I have tried doing type:[String].
But this approach makes the population methods return nothing. Following script (output at the end of the script) explains this behavior:

Script for populate not working with `[ObjectId]`. Click to expand!
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    // we're connected!
    console.log('Connected to DB!')
});

const kittySchema = new mongoose.Schema({
    doesntpopulate: {
  	  type: [mongoose.Schema.Types.ObjectId], //<----- Using type:[ObjectId] approach
  	  ref: 'features',
    },
    populatescorrectly: [{ 
  	  type: mongoose.Schema.Types.ObjectId,  // <---- Using [{type:ObjectId}] approach
  	  ref: 'features',
    }],
});

const kittyfeatures = new mongoose.Schema({
    name: String
});

const Kitten = mongoose.model('Kitten', kittySchema);
const Features = mongoose.model('features', kittyfeatures);

const testfunc = async () => {
    const features = await new Features({ name: 'something' }).save();
    const kitty = await new Kitten({ doesntpopulate: [features], populatescorrectly: [features] }).save();
    // Population method
    const find = await Kitten.findById(kitty).populate('doesntpopulate populatescorrectly'); 
    console.log(find)  }

testfunc()
// OUTPUTS
// {
// 	doesntpopulate: [],
// 	populatescorrectly: [{ _id: 604eb2c0d91f8e083fe33e44, name: 'something', __v: 0 }],
// 	_id: 604eb2c0d91f8e083fe33e45,
// 	__v: 0
// }

What is the current behavior?

Running the script below which has a schema with two fields set to select:false gives the following output.

Connected to DB!
{ shouldntshow: [ 'Silence' ], _id: 604ea14d046a2271e1542498, __v: 0 }

It should not show the shouldntshow field.

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

Here is a standalone script for this behavior.

Script for `select:false` not working inside array. Click to expand!
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    console.log('Connected to DB!')
});

const kittySchema = new mongoose.Schema({
    shouldntshow: [{ // <------ it shouldnt show because select is false
  	  type: String,
  	  select: false
    }],
    doesntshow: { // working correctly
  	  type: [String],
  	  select: false
    },
});
const Kitten = mongoose.model('Kitten', kittySchema);

const testfunc = async () => {
    const kitty = await new Kitten({
  	  shouldntshow: ['Silence'],
  	  doesntshow: ['Silence']
    }).save();

    const find = await Kitten.findById(kitty);
    console.log(find)
}

testfunc()

What is the expected behavior?

It should be this. where no fields (specific my example) should show because select:false

Connected to DB!
{ _id: 604ea236169d6c86ecf5e66a, __v: 0 }

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version..
node: v14.15.4
"mongoose": "^5.12.0"
MongoDB: 4.4.3

@IslandRhythms
Copy link
Collaborator

I believe this is what you are looking for:
https://mongoosejs.com/docs/guide.html#selectPopulatedPaths

otherwise, select is a function call of a query
https://mongoosejs.com/docs/api.html#query_Query-select

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Mar 16, 2021
@indraraj26
Copy link

Hi @IslandRhythms , he mean if i set select: false in schema still it is showing on find() query

const mongoose = require('mongoose');

let UserSchema = new mongoose.Schema({
	email: [{
        type: String,
        select: false,
    }],
});


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 UserModel = mongoose.model('user', UserSchema);
	const newUser = { email: ['test@test.com'] };
	const user = new UserModel(newUser);
	await user.save();
    const result = await UserModel.find();
    console.log(result)
}

@IslandRhythms
Copy link
Collaborator

@indraraj26 I understand, but in the docs there is no select option when creating a schema. There is a selectPopulatedPaths option which may be what they were referring to which is what I linked.

@indraraj26
Copy link

Why in this case it is working if there is no such option select: false
Array of email

let UserSchema = new mongoose.Schema({
	email: {
        type: [String],
        select: false,
    },
});

Type string

let UserSchema = new mongoose.Schema({
	email: {
        type: String,
        select: false,
    },
});

@IslandRhythms i think it was removed in latest version of mongoose or may be it is not updated in docs. I see lot of posts regarding the select: false
mongoose github issue
stackoverflow 1
stackoverflow 2

@IslandRhythms
Copy link
Collaborator

@vkarpov15 is there a select option when defining a schema? Or was it removed recently? Looking at the docs I didn't see it as a listed option.

@Jatin-MYST
Copy link
Author

@indraraj26 understanding is correct on what Im reporting here.
@IslandRhythms select in schema is definitely a thing as I have demonstrated in my second script:

const kittySchema = new mongoose.Schema({
    shouldntshow: [{ // <------ it shouldnt show because select is false
  	  type: String,
  	  select: false
    }],
    doesntshow: { // working correctly
  	  type: [String],
  	  select: false
    },
});

It could just be that it is not included in the docs.
My issue is that select:false doesnt seem to work when used in [{}].
selectPopulatedPaths and Query-select is not what I am after.

@vkarpov15 vkarpov15 added this to the 5.12.2 milestone Mar 20, 2021
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 20, 2021
vkarpov15 added a commit that referenced this issue Mar 22, 2021
vkarpov15 added a commit that referenced this issue Mar 22, 2021
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. enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

4 participants