0
点赞
收藏
分享

微信扫一扫

MongoDB 命令行

唯米天空 2022-03-23 阅读 156
服务背景:本机装的Mongo

登录mongo

mongo

查看所有数据库

show dbs;

选择数据库

use admin;

创建集合

db.createCollection("newCollection");

显示集合

show collections;

查询列表

db.${collection}.find({"字段": "数据"});

查询单条集合数据

db.${collection}.findOne({"字段": "数据"});

查询单条集合数据并格式化

db.${collection}.findOne({"字段": "数据"}).pretty();

插入数据

db.${collection}.insert({"字段": "数据", "字段1": "数据1"});

更新数据

# db.${collection}.update(where, data),此操作会覆盖整条数据
# 例如:元数据 {"username": "张三", "age": "10"}, 执行下面语句后元数据会被改成{"username": "李四"}
db.${collection}.update({"_id": "10292"}, {"username": "李四"})

更新数据

# db.${collection}.update(where, {$set: {}}),$set改单对象
# 例如:元数据 {"username": "张三", "age": "10"}, 执行下面语句后元数据会被改成{"username": "张三", "age": "11"}
db.${collection}.update({"_id": "10292"}, {$set: {"age": "11"}})


举报

相关推荐

0 条评论