find() include and exclude keys in same time

997 views
Skip to first unread message

michael Baarz

unread,
Oct 13, 2010, 8:57:07 AM10/13/10
to mongodb-user
Hello,

i have found the following info in the Definitive MongoDB Guide:


You can also use this second parameter to exclude specific key/value
pairs from the
results of a query. For instance, you may have documents with a
variety of keys, and
the only thing you know is that you never want to return the
"fatal_weakness" key:
> db.users.find({}, {"fatal_weakness" : 0})
This can even prevent "_id" from being returned:
> db.users.find({}, {"username" : 1, "_id" : 0})
{
"username" : "joe",
}


This is very nice i dont wanna the _id value. Now do something similar
in my mongodb:


> db.custs_struct.find({pid:4},{cid:1, "_id":0})
error: {
"$err" : "You cannot currently mix including and excluding fields.
Contact us if this is an issue."
}
>

Is it a bug, or a feature? How to exclude the _id value now from the
return values?

Eliot Horowitz

unread,
Oct 13, 2010, 9:43:14 AM10/13/10
to mongod...@googlegroups.com
What version are you on?
I think that was a change for 1.6

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Justin Dearing

unread,
Oct 18, 2010, 12:39:30 PM10/18/10
to mongodb-user
I just discovered this now and its kinda annoying for me. I want to
exclude dates from this subcollections:

> db.events.findOne(null, {_id:1, Name:1, "Locations":1 })
{
"_id" : ObjectId("4cbc7344b4a5ef111c1a3c6a"),
"Name" : "Bar Crawl",
"Locations" : {
"_t" :
"System.Collections.Generic.List`1[[Wunderman.EventRSVPSite.BusinessObjects.EventLocation,
EventRSVPSite.BusinessObjects]]",
"v" : [
{
"_id" :
ObjectId("4cbc7345b4a5ef111c1a3c6b"),
"Name" : "McSorley's Olde Ale House",
"Address1" : "15 East 7th Street",
"Address2" : null,
"City" : "New York",
"State" : "NY",
"Zip" : "10003",
"Capacity" : 0,
"Dates" : {
"_t" :
"System.Collections.Generic.List`1[[Wunderman.EventRSVPSite.BusinessObjects.EventDate,
EventRSVPSite.BusinessObjects]]",
"v" : [ ]
}
},
{
"_id" :
ObjectId("4cbc7345b4a5ef111c1a3c6c"),
"Name" : "O'Keefe's Bar & Grill",
"Address1" : "62 Court St",
"Address2" : null,
"City" : "Brooklyn",
"State" : "NY",
"Zip" : "11201",
"Capacity" : 0,
"Dates" : {
"_t" :
"System.Collections.Generic.List`1[[Wunderman.EventRSVPSite.BusinessObjects.EventDate,
EventRSVPSite.BusinessObjects]]",
"v" : [ ]
}
}
]
}
}


So I tried this:

> 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
}

Now I get the same results from:
db.events.findOne(null, {_id:1, Name:1, "Locations._t":1,
"Locations.v.Name":1, "Locations.v.Address1":1, "Locations.v.Address2":
1, "Locations.v.City":1, "Locations.v.State":1,"Locations.v.Zip":1 })

However, if the first form worked, it would be more readable.

Justin

On Oct 13, 9:43 am, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> What version are you on?
> I think that was a change for 1.6
>
>
>
> On Wed, Oct 13, 2010 at 8:57 AM, michael Baarz <mba...@gmail.com> wrote:
> > Hello,
>
> > i have found the following info in the Definitive MongoDB Guide:
>
> > You can also use this second parameter toexcludespecific key/value
> > pairs from the
> > results of a query. For instance, you may have documents with a
> > variety ofkeys, and
> > the only thing you know is that you never want to return the
> > "fatal_weakness" key:
> >> db.users.find({}, {"fatal_weakness" : 0})
> > This can even prevent "_id" from being returned:
> >> db.users.find({}, {"username" : 1, "_id" : 0})
> > {
> > "username" : "joe",
> > }
>
> > This is very nice i dont wanna the _id value. Now do something similar
> > in my mongodb:
>
> >> db.custs_struct.find({pid:4},{cid:1, "_id":0})
> > error: {
> >        "$err" : "You cannot currently mix including and excluding fields.
> > Contact us if this is an issue."
> > }
>
> > Is it a bug, or a feature? How toexcludethe _id value now from the

Kristina Chodorow

unread,
Oct 18, 2010, 2:19:21 PM10/18/10
to mongod...@googlegroups.com
This works in 1.6.3 & the 1.7 branch, which version are you using?

Justin Dearing

unread,
Oct 18, 2010, 2:30:08 PM10/18/10
to mongod...@googlegroups.com
1.6.3, official mongodb.org binaries, not a personal build.

From the log:

Mon Oct 18 09:31:23 MongoDB starting : pid=1932 port=27017 dbpath=/data/db/ 32-bit
Mon Oct 18 09:31:23 db version v1.6.3, pdfile version 4.5
Mon Oct 18 09:31:23 git version: 278bd2ac2f2efbee556f32c13c1b6803224d1c01
Mon Oct 18 09:31:23 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_LIB_VERSION=1_35
Mon Oct 18 09:31:24 [initandlisten] waiting for connections on port 27017
Mon Oct 18 09:31:24 [websvr] web admin interface listening on port 28017
Mon Oct 18 11:21:12 [initandlisten] connection accepted from 127.0.0.1:21715 #1
Mon Oct 18 11:21:12 [conn1] query admin.$cmd ntoreturn:1 command: { ismaster: 1 } reslen:64 349ms

Kristina Chodorow

unread,
Oct 18, 2010, 2:40:33 PM10/18/10
to mongod...@googlegroups.com
Oh, never mind.  Excluding the _id field works, but other stuff doesn't.  Agreed it would be easier, I guess this is the case to watch/vote for: http://jira.mongodb.org/browse/SERVER-391.

Justin Dearing

unread,
Oct 18, 2010, 2:46:46 PM10/18/10
to mongod...@googlegroups.com
I didn't find that when checked JIRA before, so I added a comment with the error message. Now you can find it by searching jira for "You cannot currently mix including and excluding fields. Contact us if this is an issue."

Justin
Reply all
Reply to author
Forward
0 new messages