VB.NET读写MongoDB
MongoDB是一个基于分布式文件存储的数据库。由C++编写,旨在为WEB应用程序提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。他所支持的查询语言非常强大,语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,还支持对数据建立索引。
MongoDB很好的实现了面向对象的思想,在MongoDB中每一条记录都是一个Document对象。他最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松实现CRUD操作。
Imports MongoDB.Driver.Builders Imports MongoDB.Driver Imports MongoDB.Bson初始化连接所要操作的文档集合
'所要连接的DB地址 Dim connectionString As String = "mongodb://localhost" '所要连接的DB名称 Dim databaseString As String = "DBname" Dim mongoClient As MongoClient = New MongoClient(connectionString) Dim mongoServer As MongoServer = mongoClient.GetServer() Dim mongoDatabase As MongoDatabase = mongoServer.GetDatabase(databaseString) '要进行操作的集合 Dim dbGather As MongoCollection(Of BsonDocument) = mongoDatabase.GetCollection(Of BsonDocument)("DBgather")
①新建文档及文档集合
如果数据库中没有插入的文档所在的集合,则会新建文档集合
Dim bsonDoc As BsonDocument = New BsonDocument bsonDoc("sNo") = "2102040819920120" bsonDoc("scNo") = "88888888" franswerImage.Insert(bsonDoc)②条件检索
检索一条数据
Dim bsonQuery = New QueryDocument() bsonQuery("_id") = New ObjectId("55cd5917522e341f20a3cbed") bsonQuery("scNo") = "88888888" Dim oneData As BsonDocument = dbGather.FindOne(bsonQuery)
检索所有数据
Dim allImage As MongoCursor(Of BsonDocument) = dbGather.Find(bsonQuery) For Each bson In allImage Dim studentNo as String = bson("scNo") Nextor
For Each bson In dbGather.Find(stdchoiceQuery) Dim studentNo as String = bson("scNo") Next③模糊检索
如果用到正则表达式,则需引用
Imports System.Text.RegularExpressions
‘Mongodb中模糊检索以"210302"开头的sNo字段 Dim bsonQuery = New QueryDocument() Dim scNStr As String = "210302" Dim scNQuery As BsonElement = New BsonElement("sNo", New Regex("^" + scNStr + ".*$")) bsonQuery.Add(scNQuery) Dim oneData As BsonDocument = dbGather.FindOne(bsonQuery)模糊检索,并删除符合条件的文档
Dim bsonQuery As New QueryDocument bsonQuery("sNo") = New BsonRegularExpression("/^" + "210302" + "/") dbGather.Remove(bsonQuery)检索文档中是否存在该字段
Dim bsonDoc As BsonDocument = New BsonDocument bsonDoc.Add("$exists", True) answerSheetErrorQuery.Add("sNo", bsonDoc)$in的使用
Dim bsonDoc As BsonDocument = New BsonDocument Dim bsArray As BsonArray = New BsonArray() For i = 33 To 47 bsArray.Add(i.ToString) Next bsonDoc.Add("$in", bsArray) stdscoreQuery.Add("sNo", bsonDoc)
‘通过DateTimePicker获取日期时间 Dim dateTo As Date = DateTimePicker1.Value Dim dateFromstr As String = DateTimePicker2.Value.ToString.Split(" ")(0) ‘去掉DateTimePicker2的时间,只留日期 Dim dateFrom As Date = New DateTime(dateFromstr.Split("/")(0), dateFromstr.Split("/")(1), dateFromstr.Split("/")(2), 0, 0, 0) Dim schoolNo As String = TextBox1.Text Dim testNo As String = TextBox2.Text ‘检索在dateFrom与dateTo之间的"UDT"字段 Dim stdscoreQuery As New QueryDocument() Dim fDate As BsonDocument = New BsonDocument("$lte", dateFrom) Dim tDate As BsonDocument = New BsonDocument("$gte", dateTo) Dim dateQuery As New QueryDocument() Dim df As BsonElement = New BsonElement("$lte", dateFrom) dateQuery.Add(df) Dim dt As BsonElement = New BsonElement("$gte", dateTo) dateQuery.Add(dt) stdscoreQuery.Add("UDT", dateQuery) Dim errorQuestion As MongoCursor(Of BsonDocument) = stdscore.FindOne(stdscoreQuery)
④批量更新,批量插入
有时候需要更新一组多条数据,就可以用批量更新批量插入
批量插入:
Dim bsonList As List(Of BsonDocument) = New List(Of BsonDocument) For i = 0 to 100 Dim temp As BsonDocument = New BsonDocument temp("score") = 0 temp("schoolNo") = "88888888" bsonList.Add(temp) Next Dim scoreQuery As New QueryDocument scoreQuery("tId") = "asd12ewqasd13eqwsdadsx" scoreQuery("sN") = "888888" socore.Remove(scoreQuery) socore.InsertBatch(bsonList)
批量更新:
'批量更新_id = bscanTempIdBsonArray中的数据的status为4 Dim statusUpdate As New UpdateDocument("$set", New BsonDocument("status", "4")) Dim upDateQuery As New QueryDocument Dim temp As BsonDocument = New BsonDocument Dim bscanTempIdBsonArray As BsonArray = New BsonArray() bscanTempIdBsonArray.Add(New ObjectId("55fbd4bf522e341f74b4b960")) bscanTempIdBsonArray.Add(New ObjectId("55fbd4bf522e341ac4b4bf62")) bscanTempIdBsonArray.Add(New ObjectId("55fbd4bf522ede1f74b4bs61")) bscanTempIdBsonArray.Add(New ObjectId("55fbd4bf522e342s74b4b910")) temp.Add("$in", bscanTempIdBsonArray) upDateQuery.Add("_id", temp) bscantemp.Update(upDateQuery,statusUpdate, UpdateFlags.Multi)
⑤findAndModify
Dim tempQuery As New QueryDocument Dim tempDoc As BsonDocument = New BsonDocument '检索条件 tempQuery.Add("status", 1) tempQuery.Add("decode", Nothing) '检索后更新的内容 Dim UpdateDoc As New BsonDocument UpdateDoc.Add("decode", 1) Dim tempUpdate As New UpdateDocument("$set", UpdateDoc) Dim tempUpdateArgs As New FindAndModifyArgs() tempUpdateArgs.Query = tempQuery tempUpdateArgs.Update = tempUpdate bscanWorkSpace.FindAndModify(tempUpdateArgs)