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 ...
随机推荐
- mysql5.7官网直译SQL语句优化--select语句优化
8.2 sql语句优化 大致内容如下: 8.2.1:SELECT语句的优化 8.2.2:优化子查询,派生表和试图引用 8.2.3:优化INFORMATION_SCHEMA查询 8.2.4:优化数据改变 ...
- hdu5351
题目名称:MZL's Border 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5351 题意:给出fib 1 = b,fib2 = a ; fib ...
- legend---十、thinkphp中如何进行原生sql操作
legend---十.thinkphp中如何进行原生sql操作 一.总结 一句话总结:query方法和execute方法 Db类支持原生SQL查询操作,主要包括下面两个方法: query方法 quer ...
- [jzoj 6086] [GDOI2019模拟2019.3.26] 动态半平面交 解题报告 (set+线段树)
题目链接: https://jzoj.net/senior/#main/show/6086 题目: 题解: 一群数字的最小公倍数就是对它们质因数集合中的每个质因数的指数取$max$然后相乘 这样的子树 ...
- JavaScript中Array方法总览
title: JavaScript中Array方法总览 toc: true date: 2018-10-13 12:48:14 push(x) 将x添加到数组最后,可添加多个值,返回数组长度.改变原数 ...
- sicily 1002 Anti-prime Sequences
debug了好久..各种犯错..按照某个学长的思路,终于AC了.. #include <iostream> #include <cstring> using namespace ...
- SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- EFcore笔记之创建模型
排除属性:NotMapped NotMapped:排除属性在CodeFirst的时候在数据库里不创建该属性 public class Blog { public int BlogId { get; ...
- MVC 设计模式
MVC 设计模式: 最早由 Trygve Teenskaug 在 1978 年提出,上世纪 80 年代是程序语言 Smalltalk 的一种内部架构.后来 MVC 被其他领域借鉴,成为了软件工程中的一 ...
- vue下面的scoped
scope这个属性限制了下面的css代码的作用域只在当前组件中生效,此时直接去更改elementui的属性,浏览器搜索不到这个元素的最外层的父级,于是这个东西不会生效