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 中执行各种查询操作。根据需要,可以调整和扩展查询条件。

相关推荐
程序猿John1 小时前
Mysql读写分离(2)-中间件mycat和实践方案
数据库·mysql·中间件
FreeBuf_1 小时前
美国国土安全部终止资助,CVE漏洞数据库项目面临停摆危机
数据库·安全·web安全
kinlon.liu1 小时前
使用Redis实现分布式限流
数据库·redis·分布式·缓存
神经星星2 小时前
覆盖40+主流模型及数据集,上海交大团队发布一站式蛋白质工程设计平台VenusFactory,一键部署教程已上线
数据库·人工智能·算法
uwvwko3 小时前
ctfshow——web入门191~194
前端·数据库·mysql·安全·ctf
ALe要立志成为web糕手4 小时前
数据库脱裤
数据库·windows·mysql·web安全·网络安全·adb·mssql
随记1234 小时前
Microsoft SQL Server Management 一键删除数据库所有外键
数据库·sql·sqlserver
韶博雅4 小时前
mysql表类型查询
android·数据库·mysql
小wanga4 小时前
【MySQL】索引特性
android·数据库·mysql
wpp03035 小时前
GaussDB SQL查询与子查询:从入门到性能调优
数据库