MongoDB 基本查询语句

基本查询

  1. 查询所有文档

    sh 复制代码
    db.collection.find()

    示例:

    sh 复制代码
    db.users.find()
  2. 按条件查询文档

    sh 复制代码
    db.collection.find({ key: value })

    示例:

    sh 复制代码
    db.users.find({ age: 25 })
  3. 查询并格式化输出

    sh 复制代码
    db.collection.find().pretty()

    示例:

    sh 复制代码
    db.users.find().pretty()

条件操作符

  1. 等于 ($eq)

    sh 复制代码
    db.collection.find({ key: { $eq: value } })

    示例:

    sh 复制代码
    db.users.find({ age: { $eq: 25 } })
  2. 不等于 ($ne)

    sh 复制代码
    db.collection.find({ key: { $ne: value } })

    示例:

    sh 复制代码
    db.users.find({ age: { $ne: 25 } })
  3. 大于 ($gt)

    sh 复制代码
    db.collection.find({ key: { $gt: value } })

    示例:

    sh 复制代码
    db.users.find({ age: { $gt: 25 } })
  4. 大于等于 ($gte)

    sh 复制代码
    db.collection.find({ key: { $gte: value } })

    示例:

    sh 复制代码
    db.users.find({ age: { $gte: 25 } })
  5. 小于 ($lt)

    sh 复制代码
    db.collection.find({ key: { $lt: value } })

    示例:

    sh 复制代码
    db.users.find({ age: { $lt: 25 } })
  6. 小于等于 ($lte)

    sh 复制代码
    db.collection.find({ key: { $lte: value } })

    示例:

    sh 复制代码
    db.users.find({ age: { $lte: 25 } })

逻辑操作符

  1. 与 ($and)

    sh 复制代码
    db.collection.find({ $and: [ { key1: value1 }, { key2: value2 } ] })

    示例:

    sh 复制代码
    db.users.find({ $and: [ { age: { $gt: 25 } }, { name: "Alice" } ] })
  2. 或 ($or)

    sh 复制代码
    db.collection.find({ $or: [ { key1: value1 }, { key2: value2 } ] })

    示例:

    sh 复制代码
    db.users.find({ $or: [ { age: { $lt: 25 } }, { name: "Alice" } ] })
  3. 非 ($not)

    sh 复制代码
    db.collection.find({ key: { $not: { condition } } })

    示例:

    sh 复制代码
    db.users.find({ age: { $not: { $gt: 25 } } })

嵌套文档查询

  1. 查询嵌套文档中的字段

    sh 复制代码
    db.collection.find({ "nested.key": value })

    示例:

    sh 复制代码
    db.users.find({ "address.city": "New York" })

数组操作符

  1. 数组中包含某个值 ($in)

    sh 复制代码
    db.collection.find({ key: { $in: [ value1, value2 ] } })

    示例:

    sh 复制代码
    db.users.find({ favoriteColors: { $in: [ "red", "blue" ] } })
  2. 数组中不包含某个值 ($nin)

    sh 复制代码
    db.collection.find({ key: { $nin: [ value1, value2 ] } })

    示例:

    sh 复制代码
    db.users.find({ favoriteColors: { $nin: [ "red", "blue" ] } })
  3. 数组中包含所有值 ($all)

    sh 复制代码
    db.collection.find({ key: { $all: [ value1, value2 ] } })

    示例:

    sh 复制代码
    db.users.find({ favoriteColors: { $all: [ "red", "blue" ] } })

示例查询

假设你有一个名为 mydatabase 的数据库和一个名为 users 的集合,集合中的文档结构如下:

json 复制代码
{
  "name": "Alice",
  "age": 25,
  "address": {
    "city": "New York",
    "zip": "10001"
  },
  "favoriteColors": ["red", "blue"]
}

你可以进行以下查询:

  1. 查询所有用户

    sh 复制代码
    db.users.find().pretty()
  2. 查询年龄大于 25 的用户

    sh 复制代码
    db.users.find({ age: { $gt: 25 } })
  3. 查询住在 New York 的用户

    sh 复制代码
    db.users.find({ "address.city": "New York" })
  4. 查询喜欢红色或蓝色的用户

    sh 复制代码
    db.users.find({ favoriteColors: { $in: [ "red", "blue" ] } })

这些示例展示了如何在 MongoDB 中执行各种查询操作。根据需要,可以调整和扩展查询条件。

相关推荐
Leo.yuan15 分钟前
不同数据仓库模型有什么不同?企业如何选择适合的数据仓库模型?
大数据·数据库·数据仓库·信息可视化·spark
麦兜*32 分钟前
MongoDB 6.0 新特性解读:时间序列集合与加密查询
数据库·spring boot·mongodb·spring·spring cloud·系统架构
chat2tomorrow35 分钟前
数据采集平台的起源与演进:从ETL到数据复制
大数据·数据库·数据仓库·mysql·低代码·postgresql·etl
稻草人想看远方38 分钟前
关系型数据库和非关系型数据库
数据库
考虑考虑38 分钟前
Postgerssql格式化时间
数据库·后端·postgresql
千里码aicood1 小时前
【springboot+vue】党员党建活动管理平台(源码+文档+调试+基础修改+答疑)
java·数据库·spring boot
TDengine (老段)1 小时前
TDengine 选择函数 Max() 用户手册
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
驾驭人生1 小时前
Asp .Net Core 系列:Asp .Net Core 集成 Hangfire+MySQL
数据库·mysql·.netcore
xhbh6662 小时前
不止是DELETE:MySQL多表关联删除的JOIN语法实战详解
数据库·mysql·程序员·mysql删除语句