[SERVER-391] Support both include and exclude on field filters Created: 28/Oct/09  Updated: 07/Apr/23

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

Type: New Feature Priority: Major - P3
Reporter: Mathias Stearn Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 16
Labels: QFB, asya
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-44558 wildcardProjection not available for ... Backlog
Assigned Teams:
Query Optimization
Participants:

 Description   

find({},

{a:1, b.c:0}

)



 Comments   
Comment by David Storch [ 19/May/17 ]

asya, interesting observation that this can now be achieved in the aggregation framework with sequential projection stages. I'd like to keep this open, however, since I think in the future we may want to design a more elegant way to express this kind of document transformation within a single aggregation stage.

Comment by Asya Kamsky [ 15/May/17 ]

Do we really need this? Assuming we merge projection and aggregation $project stage, I think there will be ways to get the thing described in the initial description?

{$project:{a:1,b:1}},{$addFields:{"b.c":"$$REMOVE"}}
or
{$project:{a:1,b:1}},{$project:{"b.c":0}}

Comment by Ezequiel Primo [ 21/Jan/15 ]

Hello, these are some tests that I have been doing concerning this issue:

> db.test.findOne()
{
"_id" : ObjectId("54b4fa2618c1e7c4365f43dc"),
"family" : "Smith",
"children" : [

{ "child_name" : "John" }

,

{ "child_name" : "Anna" }

]
}

And at the moment of executing the following queries:

> db.test.find(

{"family" : "Smith"}

,

{"children" : 1, "family" : 0,"_id":0}

)
error: {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}
> db.test.find(

{"family" : "Smith"}

,

{"children" : 0, "family" : 1 ,"_id":0}

)
error: {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}
> db.test.find(

{"family" : "Smith"}

,

{"children" : 0, "family" : 1 ,"_id":1}

)
error: {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}
> db.test.find(

{"family" : "Smith"}

,

{"children" : 0, "family" : 0 ,"_id":1}

)
error: {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}

In this case for instance the result is fine:

> db.test.find(

{"family" : "Smith"}

,

{"children" : 1, "family" : 1 ,"_id":0}

)
{ "family" : "Smith", "children" : [

{ "child_name" : "John" }

,

{ "child_name" : "Anna" }

] }

Comment by Antonio Quintana [ 10/Jan/14 ]

Hi! I've got this error.

My case is the following:

  • I've made a mapReduce(map(),reduce(),options) and, by construction, each document in the options.out.reduce collection has a {_id, value} structure
  • The collection has a lot of documents, so it would be better if I can query directly from there instead of running an "almost-never-ending" find+forEach to create a new structure from it
  • There's some documents in that collection (about 5%, but it's still a large amount of data and MB to transfer along the network/internet) which need some update
  • My query if more or less (for security reasons I modified the field names) like this:
    db.find({"value.Coupons":{$size:0}}, {"_id.field1":1, "_id.field2":1, "_id.field3":0, "_id.field4":0}

    )

  • As I've already said, I only need 2 of the 4 fields of the key subdocument because of network traffic (and time, obviously) reasons, and this is an issue for us because it spends twice the time we would like it to. This is the main goal of the projection operator, isn't it?
Comment by NOVALUE Mitar [ 15/Nov/13 ]

My use case is:

Publications.find(

{'importing.person._id': personId}

,

{'importing.$': 1, 'importing.$.temporaryFile': 0}

)

So I would like to get only that subdocument of importing belonging to the person, but remove temporaryFile field in those subdocument.

Comment by Justin Dearing [ 18/Oct/10 ]

The message this currently generates is:

db.events.findOne(null, {_id:1, Name:1, "Locations":1, "Locations.v.Dates": 0})

Mon Oct 18 12:23:27 uncaught exception: error {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}

Comment by Justin Dearing [ 18/Oct/10 ]

See http://groups.google.com/group/mongodb-user/browse_thread/thread/c53d99916e46ae1a/d4a79a34104377f9#d4a79a34104377f9 for commentary.

Generated at Thu Feb 08 02:53:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.