# 创建索引,优化查询速度
# 查看索引 > db.enter_room.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "room.enter_room" } ] # 创建索引 > db.enter_room.ensureIndex({room_id: 1, user_id: 1}, {background: true}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } # 查看索引 > db.enter_room.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "room.enter_room" }, { "v" : 2, "key" : { "room_id" : 1, "user_id" : 1 }, "name" : "room_id_1_user_id_1", "ns" : "room.enter_room", "background" : true } ] # 集合内数据 > db.enter_room.findOne() { "_id" : ObjectId("606c195c8340b651d1084d9b"), "enter_time" : "2021-04-06 16:18:10", "room_id" : "123845", "user_id" : "1385786", "user_count" : "12" }