MongoDB 基本查询语句

基本查询

  1. 查询所有文档

    db.collection.find()
    

    示例:

    db.users.find()
    
  2. 按条件查询文档

    db.collection.find({ key: value })
    

    示例:

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

    db.collection.find().pretty()
    

    示例:

    db.users.find().pretty()
    

条件操作符

  1. 等于 ($eq)

    db.collection.find({ key: { $eq: value } })
    

    示例:

    db.users.find({ age: { $eq: 25 } })
    
  2. 不等于 ($ne)

    db.collection.find({ key: { $ne: value } })
    

    示例:

    db.users.find({ age: { $ne: 25 } })
    
  3. 大于 ($gt)

    db.collection.find({ key: { $gt: value } })
    

    示例:

    db.users.find({ age: { $gt: 25 } })
    
  4. 大于等于 ($gte)

    db.collection.find({ key: { $gte: value } })
    

    示例:

    db.users.find({ age: { $gte: 25 } })
    
  5. 小于 ($lt)

    db.collection.find({ key: { $lt: value } })
    

    示例:

    db.users.find({ age: { $lt: 25 } })
    
  6. 小于等于 ($lte)

    db.collection.find({ key: { $lte: value } })
    

    示例:

    db.users.find({ age: { $lte: 25 } })
    

逻辑操作符

  1. 与 ($and)

    db.collection.find({ $and: [ { key1: value1 }, { key2: value2 } ] })
    

    示例:

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

    db.collection.find({ $or: [ { key1: value1 }, { key2: value2 } ] })
    

    示例:

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

    db.collection.find({ key: { $not: { condition } } })
    

    示例:

    db.users.find({ age: { $not: { $gt: 25 } } })
    

嵌套文档查询

  1. 查询嵌套文档中的字段
    db.collection.find({ "nested.key": value })
    
    示例:
    db.users.find({ "address.city": "New York" })
    

数组操作符

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

    db.collection.find({ key: { $in: [ value1, value2 ] } })
    

    示例:

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

    db.collection.find({ key: { $nin: [ value1, value2 ] } })
    

    示例:

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

    db.collection.find({ key: { $all: [ value1, value2 ] } })
    

    示例:

    db.users.find({ favoriteColors: { $all: [ "red", "blue" ] } })
    

示例查询

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

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

你可以进行以下查询:

  1. 查询所有用户

    db.users.find().pretty()
    
  2. 查询年龄大于 25 的用户

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

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

    db.users.find({ favoriteColors: { $in: [ "red", "blue" ] } })
    

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

相关推荐

  1. MongoDB 基本查询语句

    2024-07-18 11:18:01       23 阅读
  2. mongodb 基础查询使用

    2024-07-18 11:18:01       33 阅读
  3. MongoDB基础语句及练习

    2024-07-18 11:18:01       28 阅读
  4. 基本的DQL语句-单表查询

    2024-07-18 11:18:01       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 11:18:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 11:18:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 11:18:01       62 阅读
  4. Python语言-面向对象

    2024-07-18 11:18:01       72 阅读

热门阅读

  1. ubuntu 源码安装postgresql16.0

    2024-07-18 11:18:01       24 阅读
  2. 【Tomcat9正确配置server.xml请求头信息】

    2024-07-18 11:18:01       21 阅读
  3. MYSQL设计索引一般需要考虑哪些因素?

    2024-07-18 11:18:01       25 阅读
  4. 华为OD机考题(典型题回顾)

    2024-07-18 11:18:01       21 阅读
  5. 手写实现简单Redis命令客户端功能

    2024-07-18 11:18:01       18 阅读
  6. Leetcode 238. 除自身以外数组的乘积

    2024-07-18 11:18:01       23 阅读
  7. qt listview 列表文字显示不全,如何用悬浮显示?

    2024-07-18 11:18:01       18 阅读
  8. PF4J+SpringBoot

    2024-07-18 11:18:01       20 阅读
  9. CAS的原理

    2024-07-18 11:18:01       27 阅读
  10. P5717 【深基3.习8】三角形分类

    2024-07-18 11:18:01       22 阅读
  11. 【数据平台-dataworks】-问题总结

    2024-07-18 11:18:01       23 阅读
  12. spring中的依赖注入

    2024-07-18 11:18:01       21 阅读
  13. 力扣212题:单词搜索 II

    2024-07-18 11:18:01       22 阅读