MongoDB常用语句

这篇具有很好参考价值的文章主要介绍了MongoDB常用语句。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用

展示数据库

show dbs 或 show databases 

查看当前在使用的数据库

db

展示数据库下所有表

show collections 或 show tables;

终端内容过多,用该指令清屏

cls

创建和删除

如果数据库【school】不存在,则创建它,否则就切换到【school】数据库。
因为是创建了一个空的,所以它只存放在内存中,没到磁盘里。用show dbs无法查看,但db可以

use school 

删除当前使用的数据库

db.dropDatabase()  <=>  drop database school;

创建名为student的集合(用创建语句,插入内容为空也可以创建)

db.createCollection("student")
db.student.insertOne({})

删除student集合

db.student.drop()  <=>  drop table student;

查询

db.student.find().pretty()  美化输出结果

查询所有记录

db.student.find()  <=>  select * from student;

查询指定列 name、age 数据
当然 name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,如果用 false 就是排除 name,显示 name 以外的列信息。

db.student.find({}, {name: 1, age: 1});  <=>  select name, age from student;

查询去重后的一列

db.student.distinct("name")  <=>  select distict name from student;

条件查询

db.student.find({"age": 22})  		<=>  select * from student where age = 22;
db.student.find({age: {$gt: 22}})   <=>  select * from student where age > 22;
db.student.find({age: {$lt: 22}})   <=>  select * from student where age < 22;
db.student.find({age: {$gte: 25}})  <=>  select * from student where age >= 25;
db.student.find({age: {$lte: 25}})  <=>  select * from student where age <= 25;
db.student.find({age: {$ne: 25}})   <=>  select * from student where age != 25;
db.student.find({age: {$gte: 23, $lte: 25}});    <=>  select * from student where age>=23 and age <= 25;
db.student.find({name: 'zhangsan', age: 22});  	 <=>  select * from student where name = 'zhangsan' and age = 22;
db.student.find({$or: [{age: 22}, {age: 25}]});  <=>  select * from student where age = 22 or age = 25;
db.student.find({age :{$in:[22,25]}});        	 <=>  select * from student where age in (22,25);
db.student.find({createtime:{$gt:isodate("2020-11-09t00:00:00z")}});  <=>  select * from student where createtime> '2020-11-09 00:00:00'; 

db.student.aggregate({$match:{createtime:{$gte:isodate("2020-11-10t00:00:00z"),$lt:isodate("2020-11-11t00:00:00z")}}});  <=>  select * from student where createtime >= '2020-11-10 00:00:00' and createtime < '2020-11-11 00:00:00';

查询指定列

db.student.find({age: {$gt: 25}}, {name: 1, age: 1});  <=>  select name, age from student where age > 25;

模糊查询

db.student.find({name: /zhang/})   <=>  select * from student where name like '%zhang%';
db.student.find({name: /^zhang/})  <=>  select * from student where name like 'zhang%';
db.student.find({name: /zhang$/})  <=>  select * from student where name like '%zhang';

分页

# 查询前 5 条数据
db.student.find().limit(5);  <=>  select * from student limit 5;
# 查询在 6-10条 之间的数据
db.student.find().limit(10).skip(5);  <=>  select * from student limit 5,5;

查询 10 条以后的数据

db.userInfo.find().skip(10);

排序

# 升序
db.student.find().sort({age: 1});  <=>  select * from student order by age asc;
# 降序
db.student.find().sort({age: -1});  <=>  select * from student order by age desc;

聚合

db.student.find({age: {$gte: 25}}).count();  <=>  select count(*) from student where age >= 20;

db.student.aggregate({$group:{_id:null,score:{$sum:"$score"}}});  <=>  select sum(score) from student;

db.student.aggregate({$group:{_id:null,score:{$avg:"$score"}}});  <=>  select avg(score) from student;

db.student.aggregate({$match:{createtime:{$gte:isodate("2020-11-10t00:00:00z"),$lt:isodate("2020-11-11t00:00:00z")}}},{$group:{_id:null,score:{$sum:"$score"}}});  <=>  select sum(score) from student where createtime >= '2020-11-10 00:00:00' and createtime < '2020-11-11 00:00:00';

两表连接

【student】表和【stuAdress】表关联,两表关联字段是userId相等,关联条件是userId等于2102123

db.student.aggregate([
   {
     $lookup:
       {
         from: "stuAdress",
         localField: "userId",
         foreignField: "userId",
         as: "address_detail"
       }
  },
  { $match : {"userId" :"2102123"} }
])
有点相当于 select a.*,b.* from student a, stuAdress b where a.userId=b.userId and userId='2102123'

输出结果长这样

[
  {
    _id: 1,
    userId: '2102123',
    username: 'zhangsan',
    age:'32',
    address_docs: [
      {
        _id: 1,
        userId: '2102123',
        address: '黑龙江哈尔滨'
      },
      {
        _id: 3,
        userId: '2102123',
        address: '四川成都'
      }
    ]
  }
]

skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit()。文章来源地址https://www.toymoban.com/news/detail-602068.html

插入

# 新建一个实例,再插入
doc={"name":"zhangsan","age":"32","sex":"man"}
db.test.insertOne(doc)
# 数据内容直接写入语句
db.test.insertOne({"name":"zhangsan","age":"32","sex":"man"})
# 插入数据成功会返回true,而且会在数据中添加一个_id属性
db.test.insertOne({"name":"zhangsan","age":"32","sex":"man"})				(将数据插入到test表中)
db.coil_new_test.insertOne({"name":"zhangsan","age":"32","sex":"man"})		(将数据插入到coil_new_test表中)
db.coil.new.test.insertOne({"name":"zhangsan","age":"32","sex":"man"})		(将数据插入到coil.new.test表中)

到了这里,关于MongoDB常用语句的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • MongoDB基本常用命令(一)

    存放文章评论的数据存放到 MongoDB 中,数据结构参考如下: 数据库: articledb 专栏文章评论 comment 字段名称 字段含义 字段类型 备注 _id ID ObjectId或String Mongo的主键的字段 articleid 文章ID String content 评论内容 String userid 评论人ID String nickname 评论人昵称 String createdatetime 评论的日

    2024年01月24日
    浏览(22)
  • Mongodb 常用操作

    1、 查询 user_id 是否存在 2、查询 user_id = 10 的记录 3、排序 -1,按照 _id 倒排;1,按照 _id 正排 4、查看索引 5、创建索引   6、创建唯一索引 唯一索引 - MongoDB-CN-Manual https://docs.mongoing.com/indexes/index-properties/unique-indexes 7、创建 TTL 索引   给 lastModifiedDate 字段添加 TTL 索引,包含

    2024年02月13日
    浏览(18)
  • 05 Docker 安装常用软件 (mongoDB)

    目录 1. mongoDB简介 1.1 mongodb的优势 2. mongodb的安装 2.1 创建数据文件夹 2.2 备份+日志 2.3 配置文件夹  2.4 创建两个文件  --- 2.4.1 配置如下:  2.5 拉取mongodb 2.6 运行容器   2.7 进入mongodb容器 --- 2.7.0 高版本(6.0)以上是这样的 , 旧版的没研究  --- 2.7.1 查询版本 --- 2.7.2  进入amdin数据

    2024年02月16日
    浏览(29)
  • MongoDB常用的比较符号和一些功能符号

    比较符号 功能符号

    2024年02月09日
    浏览(28)
  • 【MongoDB】数据库、集合、文档常用CRUD命令

    目录 一、数据库操作 1、创建数据库操作 2、查看当前有哪些数据库 3、查看当前在使用哪个数据库 4、删除数据库 二、集合操作 1、查看有哪些集合 2、删除集合 3、创建集合 三、文档基本操作 1、插入数据 2、查询数据 3、删除数据 4、修改数据 四、文档分页查询 五、文档其

    2024年02月13日
    浏览(36)
  • python常用库之pymongo库(Python操作Mongodb数据库)| Django项目连接MongoDB方式选型(MongoEngine)

    github:https://github.com/mongodb/mongo-python-driver PyMongo用于与Python与MongoDB数据库进行交互的工具。bson包是Python的BSON格式 的实现。Pymongo包是MongoDB的本地Python驱动程序。gridfs包是gridfs 的pymongo实现。 Pymongo支持MongoDB 3.6、4.0、4.2、4.4、5.0和6.0。 总结:PyMongo 是 MongoDB 与 Django 交互的标准

    2024年02月10日
    浏览(44)
  • MongoDB常用的操作(服务器、数据库、集合)

    前面两篇已经介绍了MongoDB系统架构及其BSON数据类型。本文将讲解基本的MongoDB操作。 一、MongoDB启动命令 启动MongoDB 查看是否启动成功 重启MongoDB 关闭MongoDB 二、使用init命令操作MongoDB 启动MongoDB 查看是否启动成功 重启MongoDB 关闭MongoDB 三、启动mongodb客户端 查看该服务是否启动

    2024年02月07日
    浏览(46)
  • Docker安装常用的容器,包括MySQL,Redis,RabbitMQ,MongoDB,FDFS等

    首先安装docker 依赖库 添加docker ce的软件源信息,自行选择,可添加可不添加,如果不是阿里云或者腾讯云的,请还是添加一下吧 安装docker docker -v --查看docker版本 docker images --查看当前docker运行 sudo systemctl start docker --启动docker 在虚拟机里边 docker images --有时候权限不足 我们

    2024年02月11日
    浏览(48)
  • docker - 常用容器部署命令大全(MySQL、MongoDB、Redis、RabbitMQ、ES、Kibana、Nacos、Sentine)

    目录 一、常用容器运行指令 MySQL Redis RabbitMQ ElasticSearch  kibana  Nacos Sentinel a)未持久化部署 b)持久化部署 a)未持久化部署: b)持久化部署:   a)为了 es 和 将来要下载的 kibana 进行互联,因此需要我们去创建一个网络. b) ES 部署 Ps:如果报错 \\\"Caused by: java.nio.file.Access

    2024年01月20日
    浏览(67)
  • 【MongoDB】什么是MongoDB?MongoDB有什么特点?MongoDB的适用场景?

    MongoDB是一个 开源、高性能、支持海里数据存储的文档型数据库。 MongoDB是一个高效的非关系型数据库(不支持表关系:只能操作单表) MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的,它支持的数据结构非常松散

    2023年04月21日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包