标签:result 对象 val collect rtti 第一条 list for 取值
直接上干货
数据库连接:
MongoClient clients = new MongoClient(_ibcoptions.Value.MongoConnStr);//配置文件读取 //获取指定数据库 IMongoDatabase dbs = clients.GetDatabase(_ibcoptions.Value.MongoDbName); //获取指定集合 BsonDocument数据库文档对象 IMongoCollection<BsonDocument> coll = dbs.GetCollection<BsonDocument>("ibeaconlocus");
多添加查询:
//创建约束生成器 FilterDefinitionBuilder<BsonDocument> builderFilter = Builders<BsonDocument>.Filter; //排序生成器 SortDefinitionBuilder<BsonDocument> builderSort = Builders<BsonDocument>.Sort; //排序约束 Ascending 正序 Descending 倒序 SortDefinition<BsonDocument> sort = builderSort.Descending("timestamp"); //约束条件 FilterDefinition<BsonDocument> filter = builderFilter.And(builderFilter.Eq("userId", userid), //gte: 大于等于,lte小于等于 builderFilter.Gte("timestamp", endTime.ToString()), builderFilter.Lte("timestamp", sartTime.ToString())); //获取数据 var result = coll.Find<BsonDocument>(filter).ToList();
取值:
foreach (var item in result) { //判断是否存在 if (item.GetValue("Channel", null) != null) { channel = item.GetValue("Channel").ToString(); } //因为是倒序的所以我取的第一条 break; }
最后用的时候别忘了需引用: MongoDB.BSon与MongoDB.Driver
标签:result 对象 val collect rtti 第一条 list for 取值
原文地址:https://www.cnblogs.com/wuyabaibsd/p/11174828.html