网站地图    收藏   

主页 > 后端 > MongoDB >

mongodb使用c#驱动数据插入demo

来源:自学PHP网    时间:2019-08-01 18:22 作者:小飞侠 阅读:

[导读] mongodb使用c#驱动数据插入demo...

Mongodb提供了多种开发语言的驱动,java,python,c++,c# 等,这里选用c#驱动作为测试;

using MongoDB.Bson;
using MongoDB.Driver;
protected staticIMongoClient client;
protected staticIMongoDatabase database;
protected staticIMongoCollection collection;
//定义连接
client = new MongoClient("mongodb://127.0.0.1:27017");
//获取test数据库
database = client.GetDatabase("test");     
//获取test数据库中的集合bios
collection = database.GetCollection("bios");
var document =new BsonDocument
      {
          { "address" , newBsonDocument
            {
              { "street","2 Avenue" },
              { "zipcode","10075" },
              { "building","1480" },
              { "coord",new BsonArray { 73.9557413, 40.7720266 } }
            }
          },
          { "borough", "Manhattan"},
          { "cuisine", "Italian"},
          { "grades", new BsonArray
              {
                new BsonDocument
                {
                  { "date",new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade","A" },
                  { "score",11 }
                },
                new BsonDocument
                {
                  { "date",new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade","B" },
                  { "score",17 }
                }
              }
          },
          { "name", "Vella"},
          { "restaurant_id","41704620" }
      };方法;

最终插入结果:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace mongodbInsert
{
  class Program
  {
    protected static IMongoClient client;
    protected static IMongoDatabase database;
    protected static IMongoCollection collection; 
    static void Main(string[] args)
    {
       client = new MongoClient("mongodb://127.0.0.1:27017");
       database = client.GetDatabase("test");
       collection = database.GetCollection("bios");
       for (int i = 0; i < 14; i++)
       {
         var document = new BsonDocument
      {
          { "address" , new BsonDocument
            {
              { "street", "2 Avenue" },
              { "zipcode", "10075" },
              { "building", "1480" },
              { "coord", new BsonArray { 73.9557413, 40.7720266 } }
            }
          },
          { "borough", "Manhattan" },
          { "cuisine", "Italian" },
          { "grades", new BsonArray
              {
                new BsonDocument
                {
                  { "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade", "A" },
                  { "score", 11 }
                },
                new BsonDocument
                {
                  { "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade", "B" },
                  { "score", 17 }
                }
              }
          },
          { "name", "Vella" },
          { "restaurant_id", "41704620" }
      };
         collection.InsertOneAsync(document);
       }
       Console.WriteLine();
       Console.ReadLine();
    }
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自学php网的支持。如果你想了解更多相关内容请查看下面相关链接

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论