Mongodb 唯一索引

集合名: stock

stock 字段

{
"changeMethod": "集中竞价",
"chgPlan": false,
"stockId": "311647",
"announcementId": 31742356,
"updateTime": "2020-06-11 20:24:40.0",
"propChanges": 0.28,
"id": 186818549,
"avgPrice": 8.28,
"changeDate": "2020-06-03 00:00:00.0",
"changeType": "减持",
"closePrice": null,
"holderName": "北京代码管理合伙企业(有限合伙)",
"marketValue": 11.8,
"reportDate": "2020-06-12 00:00:00.0",
"shareChanges": "18.00",
"shareHoldAf": 487.9771,
"sharePntAf": 2.187,
"stockName": "超频一"
}

设置 stockId 为唯一索引

# 删除集合
> db.stock.drop()
# 创建
> db.createCollection("stock")
> db.stock.ensureIndex({"stockId":1},{"unique":true})
posted @ 2020-07-05 20:34  走疯  阅读(3332)  评论(0编辑  收藏  举报