https://docs.mongodb.com/getting-started/csharp/insert/

OverView

You can use the InsertOneAsync method and the InsertManyAsync method to add documents to acollection in MongoDB.

If you attempt to add documents to a collection that does not exist, MongoDB will create the collection for you.

Prerequisites

Follow the Connect to MongoDB step to connect to a running MongoDB instance and declare and define the variable _database to access the test database.

Include the following using statements.

using System;
using System.Threading.Tasks;
using MongoDB.Bson;

Insert a Document

Insert a document into a collection named restaurants.

向名为restaurants的Collection插入一条Document:  等同于关系型数据库中,向表名为restaurants的表插入一条记录

The operation will create the collection if the collection does not currently exist.

private async void button1_Click(object sender, EventArgs e)
{
MongoClient = new MongoClient();
//根据名字获取数据库
MongoDatabase = MongoClient.GetDatabase("test"); var document = new BsonDocument
{
{
"address", new BsonDocument
{
{"street", "2 Avenue"},
{"zipcode", ""},
{"building", ""},
{"coord", new BsonArray {73.9557413, 40.7720266}}
}
},
{"borough", "Manhattan"},
{"cuisine", "Italian"},
{
"grades", new BsonArray
{
new BsonDocument
{
{"date", new DateTime(, , , , , , DateTimeKind.Utc)},
{"grade", "A"},
{"score", }
},
new BsonDocument
{
{"date", new DateTime(, , , , , , DateTimeKind.Utc)},
{"grade", "B"},
{"score", }
}
}
},
{"name", "Vella"},
{"restaurant_id", ""}
}; //根据名字获取collection
var collection = MongoDatabase.GetCollection<BsonDocument>("restaurants");
await collection.InsertOneAsync(document);
}

The method does not return a result

If the document passed to the InsertOneAsync method does not contain the _id field, the driver automatically adds the field to the document and sets the field’s value to a generated ObjectId

Additional Information

n the C# Driver documentation, see InsertOneAsyncInsertManyAsync and BsonDocument.

In the MongoDB Manual, see also the Insert Documents tutorial.

SEE ALSO

The MongoDB Manual

Insert Data with C# Driver的更多相关文章

  1. Insert data from excel to database

    USE ESPA Truncate table dbo.Interface_Customer --Delete the table data but retain the structure exec ...

  2. Bulk Insert Data

    Bulk Insert Data 命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NE ...

  3. Find or Query Data with C# Driver

    https://docs.mongodb.com/getting-started/csharp/query/ Overview You can use the Find and FindAsync m ...

  4. [Oracle] Bulk Insert Data

    命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NET for .NET Framew ...

  5. bulk insert data into database with table type .net

    1. Create Table type in Sqlserver2008. CREATE TYPE dbo.WordTable as table ( [WordText] [nchar]() NUL ...

  6. 简单的sqlserver批量插入数据easy batch insert data use loop function in sqlserver

    --example 1: DECLARE @pid INT,@name NVARCHAR(50),@level INT,@i INT,@column2 INT SET @pid=0 SET @name ...

  7. django insert data into mysql

    #!/usr/bin/python # -*- coding:utf-8 -*- # @filename: search # @author:wwx399777 wuweiji # @date: 20 ...

  8. [Postgre] Insert Data into Postgre Tables

    // Insert one row INSERT INTO movies (title, release_date, count_stars, director_id) VALUES ( 'Kill ...

  9. Bulk Insert的用法 .

    /******* 导出到excel */EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S&quo ...

随机推荐

  1. C/C++ Quick Sort Algorithm

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069 快速排序算法,由C.A. ...

  2. ASP.NET-常用正则表达式

    常用正则表达式 正则: [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}", ErrorMes ...

  3. JSON数据的生成与解析

    JSON数据的生成与解析.首先先到网上下载一个json jar包,我用的是org.json 演示样例代码: package json; import org.json.JSONArray; impor ...

  4. Linux 截图

    方法一:快捷键截图 对整个屏幕截图: PrintScreen 对活动窗体截图: Alt+PrintScreen 对随意矩形截图: Shift+PrintScreen 以上三个快捷键再加上Ctrl.就会 ...

  5. 日常问题记录-- java.lang.IllegalArgumentException: taglib definition not consistent with specification version

    转自:https://www.cnblogs.com/carterzhang/p/4288650.html 背景: tomcat8.0中使用taglib 错误表现: java.lang.Illegal ...

  6. Java 8 Concurrency Tutorial--转

    Threads and Executors Welcome to the first part of my Java 8 Concurrency tutorial. This guide teache ...

  7. jqGrid的editrules参数

    原文链接:http://blog.csdn.net/mengtianyalll/article/details/13502841 editrules    editrules是用来设置一些可用于可编辑 ...

  8. C++之指针与引用,函数和数组

    ]={,,}; //ptr是指针,该指针类型是int[3] ]=&arr; cout << **ptr << endl;//第一次解指针时得到数组地址,第二次解指针取数 ...

  9. indexedDB介绍

    什么是 indexedDB IndexedDB 是一种使用浏览器存储大量数据的方法.它创造的数据可以被查询,并且可以离线使用. IndexedDB对于那些需要存储大量数据,或者是需要离线使用的程序是非 ...

  10. React 第二天

    第二天 01 关于Vue和React中key的作用 在循环的时候一定要为组件加key 02关于jsx语法的注意事项 jsx中的注释 {/*  */} class要写成className label标签 ...