Py学习  »  MongoDB

如何在MongoDB中实现子查询

Oto-obong Eshiett • 4 年前 • 364 次点击  

我有一个这样的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
 
364 次点击  
文章 [ 1 ]  |  最新文章 4 年前
BenSower
Reply   •   1 楼
BenSower    5 年前

最简单的方法是使用一个简单的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.)