MongoDB查询语句简要分析

find() 语句

启动MongoDB服务,因为mongoDB并不随系统一起启动,可能以下命令运行后会等一小段的时间才会启动完毕。
sudo service mongodb start

进入MongoDB命令行操作界面,在命令行中敲 exit 可以退出
mongo

find() 用法:db.COLLECTION_NAME.find()
> use post #创建post数据库,并向其中插入文档
> db.post.insert([
{
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'shiyanlou',
   url: 'http://www.shiyanlou.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
},
{
   title: 'NoSQL Database', 
   description: "NoSQL database doesn't have tables",
   by: 'shiyanlou',
   url: 'http://www.shiyanlou.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 20, 
   comments: [    
      {
         user:'user1',
         message: 'My first comment',
         dateCreated: new Date(2013,11,10,2,35),
         like: 0 
      }
   ]
}
])

查询数据,不加任何参数默认返回所有数据记录:
db.post.find()

pretty() 语句

pretty() 可以使查询输出的结果更美观。
db.post.find().pretty()

MongoDB中的 AND

当 find() 中传入多个键值对时,MongoDB就会将其作为 AND 查询处理。用法:db.mycol.find({ key1: value1, key2: value2 }).pretty()

db.post.find({"by":"shiyanlou","title": "MongoDB Overview"}).pretty()

MongoDB中的 OR

MongoDB中,OR 查询语句以 $or 作为关键词,用法如下:
> db.post.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

同时使用 AND 和 OR

> db.post.find({
    "likes": {$gt:10},
    $or: [
        {"by": "shiyanlou"},
        {"title": "MongoDB Overview"}
    ]
}).pretty() #{\$gt:10} 表示大于10,另外,\$lt 表示小于,\$lte 表示小于等于,\$gte 表示大于等于,\$ne 表示不等于。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Mongodb进行查询时,可以使用不同的语句来满足不同的需求。例如,要查询一个集合dimlist字段下存在url字段的数据,可以使用以下语句: db.getCollection('user').find({'dimlist.url': {$exists: true}}) 这个语句的意思是查询'user'集合dimlist字段下存在url字段的数据。 另外,如果你想要进行更复杂的查询,例如在两个集合之间进行联合查询,你可以使用聚合操作来实现。以下是一个示例查询语句: db.userInfo.aggregate([ { $lookup: { from: "userAddress", localField: "userId", foreignField: "userId", as: "address_detail" } }, { $match: { "userId": "xxxx" } } ]) 这个语句的作用是将userInfo集合的userId字段与userAddress集合的userId字段进行关联查询,并将结果存储在address_detail字段。然后,通过$match操作符来筛选出userId为"xxxx"的数据。 以上是关于Mongodb查询语句简要介绍,希望对你有所帮助。如果有任何问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mongodb常用查询语句_笔记](https://blog.csdn.net/weixin_43785299/article/details/108130739)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [MongoDB常用查询语句](https://blog.csdn.net/weixin_53571731/article/details/131667022)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值