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

BUG - Subdocument mapping that has select: false results in MongoDB Server Error #11698

Closed
Refzlund opened this issue Apr 21, 2022 · 1 comment · Fixed by #11785
Closed

BUG - Subdocument mapping that has select: false results in MongoDB Server Error #11698

Refzlund opened this issue Apr 21, 2022 · 1 comment · Fixed by #11785
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Refzlund
Copy link

Bug Report

Before creating an issue please make sure you are using the latest version of mongoose
Getting a Operaion ... timed out after 10000ms error? Read this FAQ entry: https://mongoosejs.com/docs/faq.html#operation-buffering-timed-out
Getting a x.$__y is not a function error? Read this FAQ entry: https://mongoosejs.com/docs/faq.html#not-a-function

What is the current behavior?

When a document maps a subdocumentMap({ field: 'hello', number: 0 }, { field: 'world', number: 1 } )

The subdocument contains a property with select: false for ex. numberMap({ field: 'hello' }, { field: 'world' } )

Mongoose will query:

document.findOne { _id: new ObjectId("623ec001e3d4002a123561e7") } {
  projection: {
    'subdocuments.$*.number': 0
  }
}

Resulting in a MongoDB error response:
FieldPath field names may not start with '$'. Consider using $getField or $setField.

Steps to reproduce

const subdocument = new Schema({
	selected: { type: Number }, 
	not_selected: { type: Number, select: false }
})

const document = new Schema({
	subdocument_mapping: {
		type: Map,
		of: subdocument
	}
})

const Document = mongoose.model('User', document)
mongoose.set('debug', true)
mongoose.set('debug', function (coll, method, query, doc) {
	console.log(`${coll}.${method}`, query, doc)
})

async function test() {
	await Document.find({})
}

Console log

users.find {} { projection: { 'subdocument_mapping.$*.not_selected': 0 } }
FieldPath field names may not start with '$'. Consider using $getField or $setField.
MongoServerError: FieldPath field names may not start with '$'. Consider using $getField or $setField.
    at Connection.onMessage (C:\Temp\Mongoose bug\node_modules\mongodb\lib\cmap\connection.js:203:30)
    at MessageStream.<anonymous> (C:\Temp\Mongoose bug\node_modules\mongodb\lib\cmap\connection.js:63:60)
    at MessageStream.emit (node:events:526:28)
    at processIncomingData (C:\Temp\Mongoose bug\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
    at MessageStream._write (C:\Temp\Mongoose bug\node_modules\mongodb\lib\cmap\message_stream.js:28: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 TLSSocket.ondata (node:internal/streams/readable:754:22)
    at TLSSocket.emit (node:events:526:28)

What is the expected behavior?

No errors are thrown, document is fetched with the subdocuments. The subdocument will query select: false, as expected, using the property.

What are the versions of Node.js, Mongoose and MongoDB you are using?

package.json
"mongoose": "^6.3.0"

Node.js
v16.14.2

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 22, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const {Schema} = mongoose;

const subdocument = new Schema({
	selected: { type: Number }, 
	not_selected: { type: Number, select: false }
})

const document = new Schema({
	subdocument_mapping: {
		type: Map,
		of: subdocument
	}
})

const Document = mongoose.model('User', document)
mongoose.set('debug', true)
mongoose.set('debug', function (coll, method, query, doc) {
	console.log(`${coll}.${method}`, query, doc)
})

async function test() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();
	await Document.find({})
}

test();

@vkarpov15 vkarpov15 modified the milestones: 6.3.3, 6.3.4 May 3, 2022
vkarpov15 added a commit that referenced this issue May 9, 2022
Gh-11698, Prevents .$* from being put into projections by avoiding drilling down into maps
@vkarpov15 vkarpov15 reopened this May 9, 2022
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
3 participants