Docs Menu
Docs Home
/ / /
Entity Framework
/

Write Data to MongoDB

On this page

  • Overview
  • Insert
  • Insert One Entity
  • Insert Multiple Entities
  • Update
  • Delete
  • Delete One Entity
  • Delete Multiple Entities
  • Additional Information

Entity Framework Core allows you to work with data in your application without explicitly running database commands. You can insert, update, or delete data within your application and persist those changes to MongoDB by using the SaveChanges() method.

When you call the SaveChanges() method, the Entity Framework Core Provider automatically detects any changes made to your data and runs the necessary commands to update the database by using the MongoDB Query API.

In this guide, you can see examples of how to perform common write operations on an application configured to use the MongoDB Entity Framework Core Provider.

Tip

To learn how to configure an application to use the Entity Framework Core Provider, see Configure Entity Framework Core for MongoDB.

You can use the Add() method to insert a single entity into your collection, or you can use the AddRange() method to insert multiple entities at once.

The Add() method accepts a single entity of the same type that you specified on the DbSet instance that you are modifying.

The following code uses the Add() method to add a new Planet object to the DbSet called Planets. It then calls the SaveChanges() method to insert that entity into the MongoDB collection.

db.Planets.Add(new Planet()
{
name = "Pluto",
hasRings = false,
orderFromSun = 9
});
db.SaveChanges();

The AddRange() method accepts an array of entities that you want to add to the DbSet.

The following code uses the AddRange() method to add an array of Planet objects to the DbSet called Planets. It then calls the SaveChanges() method to insert those entities into the MongoDB collection.

var planets = new[]
{
new Planet()
{
_id = ObjectId.GenerateNewId(),
name = "Pluto",
hasRings = false,
orderFromSun = 9
},
new Planet()
{
_id = ObjectId.GenerateNewId(),
name = "Scadrial",
hasRings = false,
orderFromSun = 10
}
};
db.Planets.AddRange(planets);
db.SaveChanges();

To update an entity, first retrieve the entity that you want to update. Then make the changes to that entity. The provider tracks any changes made to the entity, such as setting properties or adding and removing items from fields with list values. To save the update to MongoDB, call the SaveChanges() method. The Entity Framework Core Provider compares the updated entity with a snapshot of the entity before the change and automatically updates the collection by using the MongoDB Query API.

The following code retrieves an entity in which the name value is "Mercury", then updates the name field. The code then calls the SaveChanges() method to persist that change to the collection.

var planet = db.Planets.FirstOrDefault(p => p.name == "Mercury");
planet.name = "Mercury the first planet";
db.SaveChanges();

You can use the Remove() method to delete a single entity from your collection, or the RemoveRange() method to delete multiple entities at once.

The Remove() method accepts a single entity of the same type that you specified on the DbSet instance that you are modifying.

The following code removes a Planet entity in which the name value is "Pluto". It then calls the SaveChanges() method to delete that entity from the MongoDB collection.

var planet = db.Planets.FirstOrDefault(p => p.name == "Pluto");
db.Planets.Remove(planet);
db.SaveChanges();

The RemoveRange() method accepts an array of entities to remove from the DbSet.

The following code finds two Planet entities and adds them to an array. It then uses the RemoveRange() method to remove both entities from the DbSet. Finally, it uses the SaveChanges() method to remove those entities from the MongoDB collection.

var pluto = db.Planets.FirstOrDefault(p => p.name == "Pluto");
var scadrial = db.Planets.FirstOrDefault(p => p.name == "Scadrial");
var planets = new[] { pluto, scadrial };
db.Planets.RemoveRange(planets);
db.SaveChanges();

To learn more about the methods discussed in this guide, see the following .NET API documentation links:

  • SaveChanges()

  • Add()

  • AddRange()

  • Remove()

  • RemoveRange()

← Query Data