Re: [mongodb-user] Is it possible to replace entire array (all elements) by Update.Set() - using C#

1,598 views
Skip to first unread message

Scott Hernandez

unread,
Feb 11, 2012, 8:51:31 AM2/11/12
to mongod...@googlegroups.com

Can you provide a sample doc please?

On Feb 10, 2012 11:49 PM, "Projapati" <mohamm...@gmail.com> wrote:
Hi,
I have a document which I modify by updating few fields as follows

           var query = Query.And(Query.EQ("_id", 2345));
           var update = Update.Set("category", "Food").Set("owner",
"Bob").Set("serial", 123);
           MyCollection.Update(query, update, UpdateFlags.None);

Now one of the field is an array that holds tags like ['LA',
'NY','SF']

I need a way to replace/rewrite this tags array in the above set
command.

Is it possible?

--
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,
Feb 11, 2012, 10:41:00 AM2/11/12
to mongod...@googlegroups.com
A field can be set to any value you want, so an array is just a value like any other. If you have a tags variable of type BsonArray you can replace the entire tags array like this:

    var query = Query.EQ("_id", 2345);
    var tags = new BsonArray { "LA", "NY", "SF" };
    var update = Update
        .Set("category", "Food")
        .Set("owner", "Bob")
        .Set("tags", tags);
    collection.Update(query, update);
Reply all
Reply to author
Forward
0 new messages