Deefa
Deefa

Reputation: 219

Insert a new field into an existing collection (many documents) in MongoDB using C# driver

How can I insert a new field into a MongoDB collection using the C# Update API?

What I am trying to do: I have a HomePhoneId field that I need to insert into every document in my People collection.

Any ideas?

Upvotes: 10

Views: 9243

Answers (2)

Andrew Orsich
Andrew Orsich

Reputation: 53685

Following code should work:

collection.Update(Query.Null, Update.Set("HomePhoneId", "some value"), UpdateFlags.Multi)

Btw, here is you can look into driver documentation.

Upvotes: 9

Md Abu Zafar
Md Abu Zafar

Reputation: 152

Following code should work:

        var filterDefinition = Builders<Models.Doctor>.Filter.Where(w => w.Name!=null);
        var updateDefinition = Builders<Models.Doctor>.Update
            .Set(d => d.IsApproved, value);
        _doctorRepository.Collection.UpdateMany(filterDefinition, updateDefinition);

Upvotes: 5

Related Questions