Insert Data with C# Driver
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 InsertOneAsync, InsertManyAsync and BsonDocument.
In the MongoDB Manual, see also the Insert Documents tutorial.
SEE ALSO
Insert Data with C# Driver的更多相关文章
- Insert data from excel to database
USE ESPA Truncate table dbo.Interface_Customer --Delete the table data but retain the structure exec ...
- Bulk Insert Data
Bulk Insert Data 命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NE ...
- Find or Query Data with C# Driver
https://docs.mongodb.com/getting-started/csharp/query/ Overview You can use the Find and FindAsync m ...
- [Oracle] Bulk Insert Data
命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NET for .NET Framew ...
- bulk insert data into database with table type .net
1. Create Table type in Sqlserver2008. CREATE TYPE dbo.WordTable as table ( [WordText] [nchar]() NUL ...
- 简单的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 ...
- django insert data into mysql
#!/usr/bin/python # -*- coding:utf-8 -*- # @filename: search # @author:wwx399777 wuweiji # @date: 20 ...
- [Postgre] Insert Data into Postgre Tables
// Insert one row INSERT INTO movies (title, release_date, count_stars, director_id) VALUES ( 'Kill ...
- Bulk Insert的用法 .
/******* 导出到excel */EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S&quo ...
随机推荐
- Floodlight 中创建消息对象的方法
在 floodlight 中创建各种openflow message 和 action 等採用的是简单工厂方式.BasicFactory类(实现OFMessageFactory接口.) ...
- 零基础学python-5.2 数字表达式操作符
表达式是处理数字最主要的工具 a=1#常量 a=a+1#表达式 操作符 操作符 描写叙述 yield 生成 器函数发送协议 lambda args:expression 生成匿名函数 x if y e ...
- JDBC创建mysql连接池代码
1.底层实现类(DBConnection) package JDBC.JDBCPool.MyJDBCPool; import java.sql.Connection; import java.sql. ...
- hdu1281 棋盘游戏 --- 最大匹配
给一个矩形棋盘,上面有一些空格点,能够放象棋中的"车", 现给出空格的坐标,求最多能够放多少个"车"使他们互不攻击(依据象棋规则,每行每列至多仅仅能放一个), ...
- Java虚拟机的类载入机制
Java虚拟机类载入过程是把Class类文件载入到内存.并对Class文件里的数据进行校验.转换解析和初始化,终于形成能够被虚拟机直接使用的java类型的过程. 在载入阶段,java虚拟机须要完毕下面 ...
- sql server2008对字符串日期字段分区
近期对公司产品的日志数据库做了一个数据分区,数据库使用的是sql server 2008,这里给大家提供一个參考. 须要特别说明的是,非常多网上的样例分区字段都使用的是时间类型的.而这里因为时间字段原 ...
- ThinkPHP5.0框架开发--第10章 TP5.0验证器
ThinkPHP5.0框架开发--第10章 TP5.0验证器 第10章 TP5.0验证器 ======================================= 今日学习 1.验证器 1) 控 ...
- TensorFlow高层次机器学习API (tf.contrib.learn)
TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载csv格 ...
- vue项目中设置全局引入scss,使每个组件都可以使用变量
在Vue项目中使用scss,如果写了一套完整的有变量的scss文件.那么就需要全局引入,这样在每个组件中使用. 可以在mian.js全局引入,下面是使用方法. 1: 安装node-sass.sass- ...
- python下载网页转化成pdf
最近在学习一个网站补充一下cg基础.但是前几天网站突然访问不了了,同学推荐了waybackmachine这个网站,它定期的对网络上的页面进行缓存,但是好多图片刷不出来,很憋屈.于是网站恢复访问后决定把 ...