社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

如何在MongoDB中实现子查询

Oto-obong Eshiett • 6 年前 • 723 次点击  

我有一个这样的sql语句:

    SELECT name 
   FROM CUSTOMERS 
   WHERE ID IN (SELECT ID 
         FROM CUSTOMERS 
         WHERE SALARY > 4500) 

但我很好奇如何在MangoDB中实现相同的查询。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43441
文章 [ 1 ]  |  最新文章 6 年前
BenSower
Reply   •   1 楼
BenSower    7 年前

最简单的方法是使用一个简单的find查询 $gt (greater than) operator 这样地:

db.customers.find({salary: {$gt: 4500}}, {name: 1})

// {salary: {$gt: 4500}} will only return customer documents which have a 'customer.salary' that's greater than 4500 ($gt)
// {name: 1} will create a projection with only the name. (you might have to filter the mandatory _id field, if you only want the name, though.)