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. ArrayList 的实现原理

    ArrayList  是List接口的可变数组的实现.实现了所有可选列表的操作,并包括null值在内的所有元素.此类还提供了一些方法来操作内部用来存储列表的数组大小. ArrayList 的是实现: ...

  2. iOS UI16_数据持久化

    // // Student.h // UI16_数据持久化 // // Created by dllo on 15/8/19. // Copyright (c) 2015年 zhozhicheng. ...

  3. C#一个托付的样例

    C#中的函数能够被声明的一个托付所调用. 函数为静态方法.和托付声明的參数返回值要一致.   class Program { delegate float MathOperationDelegate( ...

  4. Leetcode--easy系列4

    #58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...

  5. Android用canvas画哆啦A梦

    先上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/diss ...

  6. android官网被封掉了,仅仅好用这个站点进谷歌了!嘎嘎

         http://developer.android.com/sdk/index.html    这个能够进去.可是必须是搜狐 .360,uc都不用特意FQ     http://173.1 ...

  7. node--19 moogose demo1

    db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...

  8. express+模板引擎构建项目时遇到的几个小问题

    1.启动项目/调试项目 项目启动用:npm start 由于每次更改路由代码后必须重启服务才可以看效果,所以为了达到热加载的效果我们安装 supervisor:全局安装也可以: npm install ...

  9. java类型与Hadoop类型之间的转换

    java基本类型与Hadoop常见基本类型的对照Long LongWritableInteger   IntWritableBoolean   BooleanWritable String Text ...

  10. SPSS学习小记

    2013年1月8日 最近一直在SPSS中处理数据,涉及到函数部分,不是太懂,特记录于此,以便翻阅.   SPSS判断字符变量中是否含有某字符串的表示方式:  (INDEX(url,'ad')>0 ...