Re: [mongodb-user] How to exclude field from sudocument in csharp driver?

488 views
Skip to first unread message
Message has been deleted

Eliot Horowitz

unread,
May 13, 2011, 10:36:12 PM5/13/11
to mongod...@googlegroups.com
In the shell it would be

find( { ... } , { "reps.voters" : 0 } )
Probably similar in c#

On Fri, May 13, 2011 at 2:15 AM, Projapati <mohamm...@gmail.com> wrote:
> I have this document. I want to read _id, date,user,  reps fields from
> top collection.
> But I don't want to read voters list from the embedded reps document
>
> How do I read this in mongodb-csharp driver?
>
> cursor.SetFields("_id", "user", "date", "reps"); <-- how to exclude
> reps.voters?
> List<Comment> cmts = cur.ToList();
>
>
> { "_id" : ObjectId("abcd"),
> "date" : ISODate("2011-05-09T02:56:40.526Z"),
> "user" : "bob",
> "reps" : [
>        {
>                "_id" : ObjectId("abcdef"),
>                "user" : "guru",
>                "text" : "We will be there"
>        },
>        {
>                "_id" : ObjectId("4dcb8079602fdd0c24927831"),
>                "likes" : 1,
>                "text" : "This is second reply.",
>                "user" : "guru",
>                "voters" : [          <-- don't want to read this
>                        "papon"
>                ]
>        }]
> }
>
> --
> 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.
>
>

Robert Stam

unread,
May 13, 2011, 11:49:41 PM5/13/11
to mongodb-user
var fields = Fields.Include("user", "date",
"reps").Exclude("reps.voters"); // _id is included by default
var cursor = collection.Find(query).SetFields(fields);

Not 100% certain you can exclude a nested field from array elements,
but if you can that would be the syntax.

On May 13, 10:36 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> In the shell it would be
>
> find( { ... } , { "reps.voters" : 0 } )
> Probably similar in c#
>

Projapati

unread,
May 13, 2011, 11:59:02 PM5/13/11
to mongodb-user
Robert,
Can you mix exclude and include field at the same time?
Thanks

Robert Stam

unread,
May 14, 2011, 10:46:59 AM5/14/11
to mongodb-user
Did you try it?

Here's what I get when I experiment using the mongo shell:

> db.test.find()
{ "_id" : ObjectId("4dce949981fc201b88c257f8"), "x" : 1, "y" : { "a" :
2, "b" : 3 } }
> db.test.find({}, {x:1})
{ "_id" : ObjectId("4dce949981fc201b88c257f8"), "x" : 1 }
> db.test.find({}, {x:1,y:1})
{ "_id" : ObjectId("4dce949981fc201b88c257f8"), "x" : 1, "y" : { "a" :
2, "b" : 3 } }
> db.test.find({}, {x:1,y:1,"y.a":0})
error: {
"$err" : "You cannot currently mix including and excluding
fields. Contact us if this is an issue.",
"code" : 10053
}

But there is one special case that is supported that you can use to
exclude the _id field if you don't want it:

> db.test.find({},{_id:0,x:1})
{ "x" : 1 }
Reply all
Reply to author
Forward
0 new messages