Let's go

MongoDB使用记录

查询

public List<CheckDataEntity> GetList()
{
     Int64 startTime = Stopwatch.GetTimestamp();//获取计时器机制中当前时间的最小单位数 可以理解为时钟周期
     var filterBuilder = Builders<CheckDataEntity>.Filter;
     var start = new DateTime(2023, 02, 06, 0, 0, 0);
     var end = (new DateTime(2023, 02, 22, 0, 0, 0));
     List<CheckDataEntity> list = checkDataService.Find(x =>x.deviceAddress == "4"& (x.createTime >= start &
     x.createTime <= end)).ToList();
     double endTime = (Stopwatch.GetTimestamp() - startTime) / (double)Stopwatch.Frequency;//获取以每秒计时周期数表示的计时器频率。此字段为只读
     return list;
}

两表关联查询

db.product.aggregate([
    {
      $lookup:
        {
          from: "order",
          localField: "_id",
          foreignField: "pid",
          as: "inventory_docs"
        }
   }
]);

 

lookup 就是使用 aggregate 的 $lookup 属性,$lookup 操作需要一个四个参数的对象,该对象的属性解释如下:

  • localField:在输入文档中的查找字段
  • from:需要连接的集合
  • foreignField:需要在from集合中查找的字段
  • as:输出的字段名字

在输出的结果中,会包含一个 inventory_docs 的字段,它会把 order 中所关联的数据在数组中展现。

三表关联查询

db.order.aggregate([{
    $lookup:
    {
      from: "product",
      localField: "pid",
      foreignField: "_id",
      as: "inventory_docs"
    }
},{
    $lookup:
    {
      from: "user",
      localField: "uid",
      foreignField: "_id",
      as: "user_docs"
    }
}]);

 

视图的使用

	db.createView( 
	"v_historicaldata", //视图名称 
	"historicaldata", //数据源
	[ 
		{ $lookup: { from: "deviceinfo", localField: "deviceAddress", foreignField: "deviceAddress", as: "dev" } }, 
		{ $project: {
		 "pressure1": { $divide: [ "$pressure1", 1000 ] },
		 "pressure2": { $divide: [ "$pressure2", 1000 ] },
		 "temperature":{ $divide: [ "$temperature", 10 ] },
		 "oilPercentage":1,
		 "createTime":1,
		 "deviceAddress":1,
		 "oilLevelHeight":{$divide:[{$subtract:["$oilLevelHeight","$height2"]},10]},
		 "height2":1,
		 "dev.fitAddress": 1,

		 }} ,

		{ $unwind: "$dev" }
	]
)
	
	
  db.createView( 
	"v_newdata", //视图名称 
	"deviceinfo", //数据源
	[ 
		{$lookup:{from: "collectiontypeinfo",localField: "id",foreignField: "deviceId",as: "collectiontype"}},
		{$lookup:{from: "newestdata",localField: "deviceAddress",foreignField: "deviceAddress",as: "newestdata"}}, 
		{
     $project:
		 {
         id: 1,
				 deviceAddress:1,
				 fitAddress:1,
				 collectiontype:{
						collectName:1,units:1,alarmLow:1,alarmHigh:1,deviceId:1
				 },
				 newestdata:{oilLevelHeight:1,pressure1:1,pressure2:1,temperature:1,height2:1,oilPercentage:1,createTime:1},
       }
		}
	,{ $unwind: "$collectiontype" }
	,{ $unwind: "$newestdata" }
	]
)

 

MongoDB时区

本身MongoDB官方是知道这个的,也有很多人反应的这一问题:DateTime timezone problem。,但官方是没有修改的意向的,官方给出的修改方法是: 在时间类型属性字段上显式标注是否为本地时间

[BsonDateTimeOptions(Kind =DateTimeKind.Local)]
public DateTime SomeDateProperty {get;set;}

这个方法虽然看起来没有什么问题,但用起来麻烦不已,一来要在一些不相关的DTO库中引入Bson的库,另一方面加标记很容易遗漏。实际上操作起来是很不方便的。

于是,有人有找到了另一种方法,那就是全局设置时间序列化的方式:

DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance;

这个方法只在MongoDb.Driver 1.x版本中有效,到了2.x后,DateTimeSerializationOptions已经被废弃了,可以使用如下方法:

var serializer = newDateTimeSerializer(DateTimeKind.Local, BsonType.DateTime);
BsonSerializer.RegisterSerializer(typeof(DateTime), serializer);

MongoDB占用磁盘大小

// 默认 byte
db.stats();
// 返回KB
db.stats(1024)
// 返回MB
db.stats(1048576)
// 返回GB
db.stats(1073741824);
// 查看单表
db.op_playback.stats(1073741824)

{
	"db" : "FEP",	                    //当前数据库名
	"collections" : 28,                 //当前数据库多少表
	"views" : 0,	
	"objects" : 303342925,	            //当前数据库所有表多少条数据
	"avgObjSize" : 912.4506189883941,	//每条数据的平均大小
	"dataSize" : 276785439682,	        //所有数据的总大小
	"storageSize" : 41067261952,	    //所有数据占的磁盘大小 
	"numExtents" : 0,
	"indexes" : 100,	                //索引数量
	"indexSize" : 97699581952,	        //索引大小 
	"ok" : 1
}

  

其他

posted @ 2023-02-21 08:23  chenze  阅读(28)  评论(0编辑  收藏  举报
有事您Q我