Docs Menu

Docs HomeC#/.NET

Insert a Document

On this page

  • Example
  • Expected Result
  • Additional Information
  • API Documentation

You can insert a single document into a collection by using the synchronous InsertOne() method, or the asynchronous InsertOneAsync() method.

The following example inserts a document into the restaurants collection.

Select the Asynchronous or Synchronous tab to see the corresponding code.

After running either of the preceding full examples, the InsertOne() method inserts the document, and the Find() method returns the newly inserted document. The output is similar to the following:

Inserting a document...
Document Inserted: { "_id" : ObjectId("..."), "name" : "Mongo's Pizza", "restaurant_id" : "12345", "cuisine" : "Pizza", "address" : { "_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson", "_v" : { "street" : "Pizza St", "zipcode" : "10003" } }, "borough" : "Manhattan", "grades" : [{ "_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson", "_v" : { } }] }

To learn more about using builders, see Operations with Builders.

  • InsertOne()

  • InsertOneAsync()

  • Find()

  • FirstOrDefault()

←  Find Multiple DocumentsInsert Multiple Documents →