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();
相关推荐
forestsea33 分钟前
MySQL 入门大全:数据类型
数据库·mysql
为自己_带盐1 小时前
浅聊一下数据库的索引优化
开发语言·数据库·php
gb42152871 小时前
mysql数据库中某个数据表的碎片率自己降低了,mysql数据表对碎片率有自动优化机制吗?
数据库·mysql
AI大模型顾潇2 小时前
[特殊字符] 本地大模型编程实战(29):用大语言模型LLM查询图数据库NEO4J(2)
前端·数据库·人工智能·语言模型·自然语言处理·prompt·neo4j
有时间要学习2 小时前
MySQL——数据类型&&表的约束
数据库·mysql
AI改变未来2 小时前
数据库常见故障排查
数据库
bing_1582 小时前
MongoDB 的核心概念(文档、集合、数据库、BSON)是什么?
数据库·mongodb·oracle
feilieren2 小时前
Windows 安装 Milvus
数据库·ai·milvus
kngines3 小时前
【PostgreSQL数据分析实战:从数据清洗到可视化全流程】附录-D. 扩展插件列表(PostGIS/PostgREST等)
数据库·postgresql·数据分析·pgvector·扩展插件·postgrest·向量数据
星星点点洲3 小时前
【Redis】谈谈Redis的设计
数据库·redis·缓存