首页 > 数据库 >MongoDB快速开始(V6.0)

MongoDB快速开始(V6.0)

时间:2022-11-19 01:11:16浏览次数:68  
标签:V6.0 MongoDB db genres collection movies 文档 快速 find

1. 切换数据库

显示当前数据库

db

切换数据库

use examples

2. 插入文档

MongoDB stores documents in collections. Collections are analogous to tables in relational databases. If a collection does not exist, MongoDB creates the collection when you first store data for that collection.

db.movies.insertMany([
   {
      title: 'Titanic',
      year: 1997,
      genres: [ 'Drama', 'Romance' ],
      rated: 'PG-13',
      languages: [ 'English', 'French', 'German', 'Swedish', 'Italian', 'Russian' ],
      released: ISODate("1997-12-19T00:00:00.000Z"),
      awards: {
         wins: 127,
         nominations: 63,
         text: 'Won 11 Oscars. Another 116 wins & 63 nominations.'
      },
      cast: [ 'Leonardo DiCaprio', 'Kate Winslet', 'Billy Zane', 'Kathy Bates' ],
      directors: [ 'James Cameron' ]
   },
   {
      title: 'The Dark Knight',
      year: 2008,
      genres: [ 'Action', 'Crime', 'Drama' ],
      rated: 'PG-13',
      languages: [ 'English', 'Mandarin' ],
      released: ISODate("2008-07-18T00:00:00.000Z"),
      awards: {
         wins: 144,
         nominations: 106,
         text: 'Won 2 Oscars. Another 142 wins & 106 nominations.'
      },
      cast: [ 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart', 'Michael Caine' ],
      directors: [ 'Christopher Nolan' ]
   }
])

3. 查询所有文档

To select the documents from a collection, you can use the db.collection.find() method. To select all documents in the collection, pass an empty document as the query filter document to the method.

In the shell, copy and paste the following to return all documents in the movies collection.

db.movies.find( { } )

4. 过滤数据

查找导演为“Christopher Nolan”的文档

db.movies.find( { "directors": "Christopher Nolan" } );

查找2000年前上映的文档

db.movies.find( { "released": { $lt: ISODate("2000-01-01") } } );

查找获奖数目超过一百的文档

db.movies.find( { "awards.wins": { $gt: 100 } } );

查找语言为中文或者日语的电影文档

db.movies.find( { "languages": { $in: [ "Japanese", "Mandarin" ] } } )

5. 投影字段(Project Field)

查询文档,只返回标题、导演和年份字段

db.movies.find( { }, { "title": 1, "directors": 1, "year": 1 } );

查询文档,返回标题和分类字段,忽略id字段

db.movies.find( { }, { "_id": 0, "title": 1, "genres": 1 } );

6. 聚合查询

While find() operations are useful for data retrieval, the aggregation pipeline allows you to manipulate data, perform calculations, and write more expressive queries than simple CRUD operations.

统计所有电影类型的的数量

db.movies.aggregate( [
   { $unwind: "$genres" },
   {
     $group: {
       _id: "$genres",
       genreCount: { $count: { } }
     }
   },
   { $sort: { "genreCount": -1 } }
] )

标签:V6.0,MongoDB,db,genres,collection,movies,文档,快速,find
From: https://www.cnblogs.com/llazycat/p/16905339.html

相关文章

  • 配置实现介绍 快速开发平台
    建立一套基础的系统开发架构,利用XML配置(利用结构化的xml文档配置相关的信息)实现增删改查及业务流程,支持开发人员方便的开发实现客户需求,支持系统实施维护人员对配置实现进行......
  • 适易快速开发平台
    建立一个基础的系统架构,利用XML配置(利用结构化的xml文档配置相关的信息)实现增删改查及业务流程,支持开发人员方便的开发实现客户需求,支持系统实施维护人员对配置实现进行维护......
  • 配置实现-适易快速开发架构实现功能总结
    马上到年关了,先总结下自己的产品吧。配置实现,自己最初的目标就是利用配置(现在用的是xml配置)实现查询列表、展现、导出及简单的编辑。从2008年积累到现在发现可用通过配置实......
  • 配置实现-快速开发实现信息管理系统
    经过3年多的积累,自己基于XML配置实现增删改查,建立了一套基础的系统架构,利用XML配置(利用结构化的xml文档配置相关的信息)实现增删改查及业务流程,支持开发人员方便的开发实现客......
  • springboot+minio实现分片上传(超大文件快速上传)
    ​ 设计由来在实际的项目开发中常遇到超大附件上传的情况,有时候客户会上传GB大小的文件,如果按照普通的MultipartFile方式来接收上传的文件,那么无疑会把服务器给干崩......
  • 如何快速优化事务性(OLTP)SQL Server性能?
    如我们所知,事务性(OLTP)SQLServer是很大部分医疗机构会选择的数据库之一,使用频率很高,发生卡、慢的机率也随之增加,那我们如何快速的对它进行性能优化呢?下面我们将利用几个重......
  • python-飞机大战.pygame快速入门-项目实战
    目标项目准备使用​​pygame​​创建图形窗口理解图像并实现图像绘制理解游戏循环和游戏时钟理解精灵和精灵组项目准备新建飞机大战项目新建一个​​hm_01_pyg......
  • 路飞项目快速查询目录
    路飞项目快速查询目录 项目开发流程pip换源python中虚拟环境的搭建数据库配置限)不用root用户操作数据库了(为数据库创建用户,并赋予权路飞项目的user表配置路飞前台......
  • window平台快速部署syslog服务器
    最近内部测试需要部署一套syslog服务器,网上找了下,无论是linux还是windows,部署均非常麻烦,现在和大家分享一个基于windows平台快速部署syslog服务器的方法这里用到的工具就......
  • 配置实现快速程序开发
    时光荏苒飞梭,今日已到三十有六回想十二年的程序开发之路,唯有近几年收获颇丰。1、从2008年开始自己积累了自己的基于配置实现的开发架构2、这几年所做的项目都有了积累并不断......