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. 阅读《Android 从入门到精通》(15)——数字时钟

    数字时钟(DigitalClock) java.lang.Object; android.view.View; android.widget.TextView; android.widget.Digi ...

  2. Windows 8.1硬盘安装Ubuntu 14.04双系统

    Windows 8.1硬盘安装Ubuntu 14.04双系统 学习了: http://www.jb51.net/os/windows/298507.html http://www.linuxidc.c ...

  3. PHP静态延迟绑定简单演示样例

    没怎么用过这个新特性.事实上也不算新啦,试试吧,如今静态类的继承非常方便了 <?php class A { protected static $def = '123456'; public st ...

  4. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. oracle日常维护语句

    1.如何查看数据库的状态    unix下 ps -ef | grep ora windows下 看服务是否起来 是否可以连上数据库 SQL> select status, instance_r ...

  6. [luogu P1776] 宝物筛选 解题报告(单调队列优化DP)

    题目链接: https://www.luogu.org/problemnew/show/P1776 题目: 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF ...

  7. Binary Indexed Tree

    我借鉴了这个视频中的讲解的填坑法,我认为非常易于理解.有FQ能力和基本英语听力能力请直接去看视频,并不需要继续阅读. naive 算法 考虑一个这样的场景: 给定一个int数组, 我们想知道它的连续子 ...

  8. Kali linux 2016.2(Rolling)中的Exploits模块详解

    简单来将,这个Exploits模块,就是针对不同的已知漏洞的利用程序. root@kali:~# msfconsole Unable to handle kernel NULL pointer der ...

  9. quartz.net定时任务框架详解

    C#做定时任务:一是Timer:而是Quartz.net:本文就学习一下Quartz.net框架 Quartz.net非常的灵活,开发人员能用很少的代码就能完成“每隔一小时执行”.“每天2点执行”.“ ...

  10. ajax返回数据时,如何将javascript值(通常为对象或数组)转为json字符串

    ajax获取值时,返回的数据为空时 alert后出现 [ ]; 用if语句判断时不为空,此时如何判断返回的数据是否为空.可将返回的值转化为json字符串. JSON.stringify() 方法用于将 ...