Mongodb 常用操作

// 查询 user_id 是否存在

sql 复制代码
db.getCollection("t_mongo_user").find({"user_id" : { $exists: true }})

// 查询 user_id = 10 的记录

sql 复制代码
db.getCollection("t_mongo_user").find({"user_id" : 10})

// 排序 -1,按照 _id 倒排;1,按照 _id 正排

sql 复制代码
db.getCollection("t_mongo_user").find({}).sort({"_id" : -1})

// 查看索引

sql 复制代码
db.getCollection("t_mongo_user_message").getIndexes();

// 创建索引

sql 复制代码
db.t_mongo_user.createIndex({"user_id":1}, {background:true})

// 创建唯一索引

sql 复制代码
db.getCollection("t_mongo_user").createIndex({
    "user_id": 1
}, {
    name: "idx_userId",
    background: true,
    unique: true
});

唯一索引 - MongoDB-CN-Manualhttps://docs.mongoing.com/indexes/index-properties/unique-indexes

// 创建 TTL 索引

给 lastModifiedDate 字段添加 TTL 索引,包含 lastModifiedDate 的文档将在 3600s 之后被删除。

sql 复制代码
db.mongo.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 } )

TTL 索引 - MongoDB-CN-Manualhttps://docs.mongoing.com/indexes/index-properties/ttl-indexes

// 删除索引

sql 复制代码
db.t_mongo_user.dropIndex("idx_user_id")

// 创建分片

sql 复制代码
sh.shardCollection("mongo.t_mongo_user", { "user_id" : "hashed" });

分片键 - MongoDB-CN-Manualhttps://docs.mongoing.com/fen-pian/shard-keys

// 创建集合

sql 复制代码
db.createCollection("t_mongo_user");

// 删除集合

sql 复制代码
db.t_mongo_user.drop();
相关推荐
AAA修煤气灶刘哥4 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud9 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术12 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug16 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom16 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*16 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰16 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*16 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
-Xie-16 小时前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存