博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB插入和批量插入
阅读量:6292 次
发布时间:2019-06-22

本文共 3075 字,大约阅读时间需要 10 分钟。

hot3.png

namespace MongoDBTest {     using System;     using System.Collections.Generic;     using System.Linq;     using System.Text;     using MongoDB.Driver;     using MongoDB.Bson;      internal class LoginDemo     {         MongoDatabase db;         MongoCollection collections;         public LoginDemo()         {             MongoServerSettings set = new MongoServerSettings()             {                 Server = new MongoServerAddress("127.0.0.1")             };             MongoServer server = new MongoServer(set);             db = server.GetDatabase("mydb");             collections = db.GetCollection("login");         }          ///          /// 单个对象插入         ///          public void InsertLogin()         {             var Time = DateTime.Now.ToUniversalTime();              //实例一 添加匿名对象             var login = new { _id = "newid_100002", time = Time, userid = 10002, sessionid = "20110829215102", ip = "192.168.0.2", title = "注册", url = "Register.aspx" };             collections.Insert(login);//插入成功              //添加一个BsonDocument对象             BsonDocument doc = new BsonDocument();             doc.Add("_id", BsonValue.Create("newid_100003"));             doc.Add("time", BsonValue.Create(Time));             doc.Add("userid", BsonValue.Create(10003));             doc.Add("sessionid", BsonValue.Create("20110829215103"));             doc.Add("ip", BsonValue.Create("192.168.0.3"));             doc.Add("title", BsonValue.Create("注册"));             doc.Add("url", BsonValue.Create("Register.aspx"));             collections.Insert(doc);//插入成功              //添加一个对象             Login man = new Login();             man._id = "newid_100004";             man.time = Time;             man.userid = 10004;             man.sessionid = "20110829215104";             man.ip = "192.168.0.4";             man.title = "注册";             man.url = "Register.aspx";             collections.Insert(man);//插入成功          }         ///          /// 批量插入         ///          public void InsertBatchLogin()         {             var Time = DateTime.Now.ToUniversalTime();             List
logins = new List
(); for (int i = 0; i < 100; i++) { Login man = new Login(); man._id = "newid_100001" + i.ToString();//_id在批量插入的时候不能重复,如果有一个重复全部集合无法插入到集合 man.time = Time; man.userid = 10004 + i; man.sessionid = "20110829215104"; man.ip = "192.168.0.4"; man.title = "注册"; man.url = "Register.aspx"; logins.Add(man); } collections.InsertBatch(typeof(Login), logins); //插入成功 } } public class Login { public string _id { get; set; } public DateTime time { get; set; } public int userid { get; set; } public string sessionid { get; set; } public string ip { get; set; } public string title { get; set; } public string url { get; set; } } }

转载于:https://my.oschina.net/u/200350/blog/87602

你可能感兴趣的文章
Git使用教程
查看>>
使用shell脚本自动监控后台进程,并能自动重启
查看>>
Flex&Bison手册
查看>>
solrCloud+tomcat+zookeeper集群配置
查看>>
/etc/fstab,/etc/mtab,和 /proc/mounts
查看>>
Apache kafka 简介
查看>>
socket通信Demo
查看>>
技术人员的焦虑
查看>>
js 判断整数
查看>>
mongodb $exists
查看>>
js实现页面跳转的几种方式
查看>>
sbt笔记一 hello-sbt
查看>>
常用链接
查看>>
pitfall override private method
查看>>
!important 和 * ----hack
查看>>
聊天界面图文混排
查看>>
控件的拖动
查看>>
svn eclipse unable to load default svn client的解决办法
查看>>
Android.mk 文件语法详解
查看>>
QT liunx 工具下载
查看>>