[SERVER-28556] positional operator in projection fails when combined with other array fields Created: 30/Mar/17  Updated: 31/May/17  Resolved: 04/May/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Mor [X] Assignee: Mark Agarunov
Resolution: Incomplete Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

1. Create a collection with documents containing to array fields with inner documents in each.
2. Query for:

db.collection.find({ "array1" : { $elemMatch : { "key1" : "value1", "key2" : "value2" } } }, { "array1.$" : 1, "array2.other_field" : 1 })

Participants:

 Description   

Combining a positional operator in the projection with some other array dotted field returns:
"BadValue: positional operator element mismatch"

Example:

db.collection.find({ "my_array" : { $elemMatch : { "key1" : "value1", "key2" : "value2" } } }, { "my_array.$" : 1, "some_other_array.other_field" : 1 })

returns the error message.

db.collection.find({ "my_array" : { $elemMatch : { "key1" : "value1", "key2" : "value2" } } }, { "my_array.$" : 1 })

returns the expected result.

According to the docs at:
https://docs.mongodb.com/manual/reference/operator/projection/positional/

Only one array field, the one being limited with the $ projection operator, should appear in the query document. Additional array fields in the query document may lead to undefined behavior

But there is no similar limit on the projection document.



 Comments   
Comment by Mark Agarunov [ 04/May/17 ]

Hello Marmor,

We haven’t heard back from you for some time, so I’m going to mark this ticket as resolved. If this is still an issue for you, please provide additional information and we will reopen the ticket.

Thanks,
Mark

Comment by Ramon Fernandez Marina [ 31/Mar/17 ]

Marmor, in what version of MongoDB are you seeing this behavior?

Comment by Mor [X] [ 30/Mar/17 ]

A workaround is to use $elemMatch in the projection, and copy the same element query document used in the query for the projection, that works but cumbersome.
Also, this workaround doesn't help if you want a specific field within the element, such as:

{ "my_array.$.key1" : 1, "other_array.key1" : 1 }

Generated at Thu Feb 08 04:18:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.