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

  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB Compass是MongoDB官方提供的一款可视化管理工具,类似于navcat,可以帮助用户更方便地进行MongoDB数据库的查询和管理操作。对于基本查询语句,可以使用MongoDB Compass提供的界面来构建查询条件,并直接执行查询操作。在查询条件中,可以使用各种查询操作符(如等于、大于、小于等)和逻辑操作符(如and、or)来构建复杂的查询语句。通过在查询条件中指定字段和对应的条件,可以快速筛选出符合条件的文档。例如,可以使用eq操作符来进行等于条件的查询,如Filters.eq("name", "小明")表示查询name字段等于"小明"的文档。对于更复杂的查询语句,可以使用and、or等逻辑操作符来组合多个条件,如Filters.and(Filters.eq("task_id", 1001), Filters.eq("data_id", 2001))表示同时满足task_id等于1001和data_id等于2001的文档。通过在MongoDB Compass中构建查询条件,并执行查询操作,可以方便地获取所需的数据结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MongoDB可视化工具MongoDB Compass导出JAVA语句](https://blog.csdn.net/m0_37821003/article/details/120526529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值