【Mongo】Mongo表结构设计以及查询示例

发布时间 2023-11-24 14:34:40作者: Angel挤一挤

 

 

Mongo SQL:

 //建表语句
 db.createCollection("pro_alter_info")


//插入文档语句
db.pro_alter_info.insert(
    {
        "alterTime": "2022-03-27 16:43:09",
        "alterType": 1,
        "proId": 2203271021000001,
        "venderId": 1,
        "alterId": 22032710000000001,
        "storeId": -1,
        "sku": -1,
        "proReward":-1, 
        "skuSnapshoot": [121267, 121278],
        "storeSnapshoot": [112641, 106499]
    }
)


db.pro_alter_info.find()

db.pro_alter_info.find({"_id":ObjectId("622723944a1756d2dca1a6c1")})
 
//查找 alterId为空的无效数据集   $exists 无法利用到索引, 但 $NE 和 $in 可以用上索引
db.pro_alter_info.find({alterId:{$in:[null]}})

//删除 alterId为空的记录  remove仅返回删除影响行数 和是否删除成功
db.pro_alter_info.remove({alterId:{$in:[null]}});

//多条件查询
db.pro_alter_info.find({proId:{$in:[2203271021000001]},sku: -1 ,alertUserName:"yingxiao" })

//时间查询1 查询 > 2022-03-27 14:00:00  差8个时区
db.pro_alter_info.find({alterTime:{$gt:ISODate("2022-03-27T06:00:00Z")}});


//时间查询2 范围查询   2022-03-27 14:00:00   < alterTime < 2022-03-27 18:00:00
db.pro_alter_info.find({alterTime:{$gt:ISODate("2022-03-27T06:00:00Z") , $lt:ISODate("2022-03-27T10:00:00Z")}});



//建索引  
db.pro_alter_info.createIndex({"alterId":1});
db.pro_alter_info.createIndex({"proId":1,"alterTime":-1});
db.pro_alter_info.createIndex({"storeId":1,"skuSnapshoot":1,"alterType":1,"proId":1,"alterTime":-1});
db.pro_alter_info.createIndex({"storeSnapshoot":1,"sku":1,"alterType":1,"proId":1,"alterTime":-1});
db.pro_alter_info.createIndex({"storeId":1,"alterType":1,"proId":1,"alterTime":-1});
db.pro_alter_info.createIndex({"storeSnapshoot":1,"alterType":1,"proId":1,"alterTime":-1});
//过期索引(一年过期)
db.pro_alter_info.createIndex({"alterTime":1},{{ expireAfterSeconds: 31536000 }});

//这个建立不成功,因为cannot index parallel arrays 建立联合索引的字段不能均为数组
db.pro_alter_info.createIndex({"storeSnapshoot":1,"skuSnapshoot":1,"alterType":1,"proId":1,"alterTime":-1})


 

 

 
alertId
venderId
sku
reward
storeId
proId
alterType
alertTime
alertUserId
alertUserName
skuSnapshoot
storeSnapshoot
skuRewardSnapshoot
记录说明
1
 
86
-1
 
-1
 
-1
 
2101092013000005
 
1 PRO_CREATE
 
2021-01-09
16:20:31
1
 
二狗子
 
[skus]
 
[storeIds]
 
 
<sku:reward>
Map的JSON字符串
 
促销运营审核通过,即促销创建
记录
促销创建时刻sku快照
促销创建时刻store快照
2
86
-1
-1
-1
2101092013000005
2 PRO_END
 
2021-01-09
16:20:31
1
二狗子
[skus]
[storeIds]
 
<sku:reward>
Map的JSON字符串
促销结束
记录 促销结束时刻sku快照
促销结束时刻store快照
3
86
-1
-1
-1
2101092013000005
3 PRO_STOP
2021-01-09
16:20:31
1
二狗子
[skus]
 
[storeIds]
 
<sku:reward>
Map的JSON字符串
强制结束
4
86
sku1
 
加商品这一刻的该品优惠设置
-1
 
2101092013000005
4 ADD_GOODS
2021-01-09
16:20:31
1
二狗子
 
[storeIds]
 
 
添加商品
 
5
86
sku1
-1
-1
2101092013000005
5 DEL_GOODS
2021-01-09
16:20:31
1
二狗子
 
[storeIds]
 
 
删除商品
6
86
sku1
调价格这一刻的该品优惠设置
-1
2101092013000005
6 PRICE_ALTER
2021-01-09
16:20:31
1
二狗子
 
[storeIds]
 
 
价格调整
7
86
-1
-1
-1
2101092013000005
7 TIME_ALTER
 
2021-01-09
16:20:31
1
二狗子
[skus]
[storeIds]
 
 
<sku:reward>
Map的JSON字符串
修改促销时间
记录
修改时间时刻的sku快照
修改时间时刻的store快照
8
86
-1
-1
storeId1
2101092013000005
8 ADD_STORE
2021-01-09
16:20:31
1
二狗子
[skus]
 
 
<sku:reward>
Map的JSON字符串
新增门店
9
86
-1
-1
storeId1
2101092013000005
9 DEL_STORE
 
2021-01-09
16:20:31
1
二狗子
[skus]
   
删除门店