MongoDB - 查询操作符:比较查询、逻辑查询、元素查询、数组查询


参考官方文档:https://www.mongodb.com/zh-cn/docs/manual/reference/operator/query/and/

1. 构造数据

① 批量插入4个文档到user集合:

db.user.insertMany([
  { name: "Alice", age: 25, email: "alice@example.com", hobbies: ["reading", "writing", "music"] },
  { name: "John", age: 30, email: "John@qq.com", hobbies: ["reading", "gaming", "traveling"] },
  { name: "Jane", age: 25, email: "Jane@qq.com", hobbies: ["sports", "music", "cooking"] },
  { name: "Mike", age: 35, email: "Mike@qq.com", hobbies: ["reading", "writing", "painting"] }
]);

② SpringBoot整合MongoDB实现批量插入文档:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
@AllArgsConstructor
@NoArgsConstructor
@Data
@Document(collection = "user")
public class User {
    @Id
    private String id;
    private String name;
    private Integer age;
    private String email;
    private List<String> hobbies;

    public User(String name,Integer age,String email,List<String> hobbies){
        this.name = name;
        this.age = age;
        this.email = email;
        this.hobbies = hobbies;
    }
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void insertUser() {
        List<User> users = Arrays.asList(
                new User("Alice", 25, "alice@example.com",Arrays.asList("reading", "writing", "music")),
                new User("John", 30, "John@qq.com", Arrays.asList("reading", "gaming", "traveling")),
                new User("Jane", 25, "Jane@qq.com", Arrays.asList("sports", "music", "cooking")),
                new User("Mike", 35, "Mike@qq.com", Arrays.asList("reading", "writing", "painting"))
        );
        mongoTemplate.insertAll(users);
    }
}

在这里插入图片描述

2. MongoDB 比较查询操作符

比较操作符根据数值比较返回数据。MongoDB提供了一些比较查询操作符,用于在查询中进行条件比较。以下是一些常用的比较查询操作符:

$eq:等于,用于匹配字段值等于指定值的文档。
例如:db.collection.find({ field: { $eq: value } })

$ne:不等于,用于匹配字段值不等于指定值的文档。
例如:db.collection.find({ field: { $ne: value } })

$gt:大于,用于匹配字段值大于指定值的文档。
例如:db.collection.find({ field: { $gt: value } })

$gte:大于等于,用于匹配字段值大于等于指定值的文档。
例如:db.collection.find({ field: { $gte: value } })

$lt:小于,用于匹配字段值小于指定值的文档。
例如:db.collection.find({ field: { $lt: value } })

$lte:小于等于,用于匹配字段值小于等于指定值的文档。
例如:db.collection.find({ field: { $lte: value } })

$in:在指定值数组中,用于匹配字段值在指定值数组中的文档。
例如:db.collection.find({ field: { $in: [value1, value2, …] } })

$nin:不在指定值数组中,用于匹配字段值不在指定值数组中的文档。
例如:db.collection.find({ field: { $nin: [value1, value2, …] } })

这些比较查询操作符可以与逻辑操作符(如 a n d 、 and、 andor、$not等)结合使用,以构建更复杂的查询条件。

1. $eq 等于

用于匹配字段值等于指定值的文档:

db.collection.find({ field: { $eq: value } })
# 相当于
db.collection.find({ field : value})
1.1 等于指定值

查询user集合中name等于Alice的文档:

db.user.find({ name: { $eq: "Alice" } })
@Test
public void findUser1() {
    // 创建查询条件
    Criteria criteria = Criteria.where("name").is("Alice");
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);
    
    users.forEach(System.out::println); 
	// User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
}
1.2 嵌入式文档中的字段等于某个值
db.inventory.insertMany( [
   { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] },
   { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
   { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
   { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] },
   { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
] )

查询 inventory 集合中 item 文档的 name 等于 "ab" 的所有文档。要对嵌入式文档中的字段指定条件,使用点符号:

db.inventory.find( { "item.name": { $eq: "ab" } } )
@Data
@Document(collection = "inventory")
public class Inventory {
    @Id
    private String id;
    private Item item;
    private int qty;
    private List<Object> tags;

    @Data
    public static class Item {
        private String name;
        private String code;
    }
}

@Test
public void findUser1() {
    // 创建查询条件
    Criteria criteria = Criteria.where("item.name").is("ab");
    // 创建查询对象
    Query query = new Query(criteria);
    List<Inventory> inventoryList = mongoTemplate.find(query, Inventory.class);

    inventoryList.forEach(System.out::println);
    // Inventory(id=1.0, item=Inventory.Item(name=ab, code=123), qty=15, tags=[A, B, C])
}
1.3 数组元素等于某个值

查询 inventory 集合中 tags 数组包含值为 "C" 的元素的所有文档:

db.inventory.find( { tags: { $eq: "B" } } )
db.inventory.find( { tags: "B" } )
@Test
public void findUser1() {
    // 创建查询条件
    Criteria criteria = Criteria.where("tags").is("C");
    // 创建查询对象
    Query query = new Query(criteria);
    List<Inventory> inventoryList = mongoTemplate.find(query, Inventory.class);

    inventoryList.forEach(System.out::println);
    // Inventory(id=1.0, item=Inventory.Item(name=ab, code=123), qty=15, tags=[A, B, C])
    // Inventory(id=5.0, item=Inventory.Item(name=mn, code=000), qty=20, tags=[[A, B], C])
}
1.4 数组元素等于数组值

查询 inventory 集合中 tags 数组与指定数组完全相同或 tags 数组包含数组 [ "A", "B" ] 的所有文档:

db.inventory.find( { tags: { $eq: [ "A", "B" ] } } )
db.inventory.find( { tags: [ "A", "B" ] } )
@Test
public void findUser1() {
    // 创建查询条件
    Criteria criteria = Criteria.where("tags").is(Arrays.asList("A","B"));
    // 创建查询对象
    Query query = new Query(criteria);
    List<Inventory> inventoryList = mongoTemplate.find(query, Inventory.class);

    inventoryList.forEach(System.out::println);
    // Inventory(id=3.0, item=Inventory.Item(name=ij, code=456), qty=25, tags=[A, B])
    // Inventory(id=5.0, item=Inventory.Item(name=mn, code=000), qty=20, tags=[[A, B], C])
}

2. $ne 不等于

用于匹配字段值不等于指定值的文档:

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

查询user集合中name不等于Alice的文档:

db.user.find({ name: { $ne: "Alice" } })
@Test
public void findUser2() {
    // 创建查询条件
    Criteria criteria = Criteria.where("name").ne("Alice");
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);
    
    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}

3. $gt 大于

用于匹配字段值大于指定值的文档:

db.collection.find({ field: { $gt: value } })
3.1 匹配文档字段

查询user集合中age大于30的文档:

@Test
public void findUser2() {
    // 创建查询条件
    Criteria criteria = Criteria.where("age").gt(30);
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);
    
    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}
3.2 根据嵌入式文档字段执行更新
db.inventory.insertMany( [
   {
      "item": "nuts", "quantity": 30,
      "carrier": { "name": "Shipit", "fee": 3 }
   },
   {
      "item": "bolts", "quantity": 50,
      "carrier": { "name": "Shipit", "fee": 4 }
   },
   {
      "item": "washers", "quantity": 10,
      "carrier": { "name": "Shipit", "fee": 1 }
   }
] )

查询 inventory 集合中 carrier 文档的 fee 大于 2 的第一个文档,并设置文档的price等于9.99:

db.inventory.updateOne(
   { "carrier.fee": { $gt: 2 } }, { $set: { "price": 9.99 } }
)
@Data
@Document(collection = "inventory")
public class Inventory {
    @Id
    private String id;
    private String item;
    private int quantity;
    private Carrier carrier;

    @Data
    public static class Carrier {
        private String name;
        private int fee;
    }
}

@Test
public void updateUser(){
    // 创建查询条件
    Criteria criteria = Criteria.where("carrier.fee").gt(2);
    // 创建更新对象
    Update update = new Update();
    update.set("price", 9.99);
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行更新操作
    UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Inventory.class);
}

对应查询结果为:

{
  _id: ObjectId("61ba3ec9fe687fce2f042417"),
  item: 'nuts',
  quantity: 30,
  carrier: { name: 'Shipit', fee: 3 },
  price: 9.99
},
{
  _id: ObjectId("61ba3ec9fe687fce2f042418"),
  item: 'bolts',
  quantity: 50,
  carrier: { name: 'Shipit', fee: 4 }
},
{
  _id: ObjectId("61ba3ec9fe687fce2f042419"),
  item: 'washers',
  quantity: 10,
  carrier: { name: 'Shipit', fee: 1 }
}

要在carrier.fee 所有大于 2 的文档中设置 price 字段的值,请使用 updateMany()

4. $gte 大于等于

用于匹配字段值大于等于指定值的文档:

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

查询user集合中age大于等于30的文档:

@Test
public void findUser2() {
    // 创建查询条件
    Criteria criteria = Criteria.where("age").gte(30);
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}

5. $lt 小于

用于匹配字段值小于指定值的文档:

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

查询user集合中age小于30的文档:

@Test
public void findUser1() {
    // 创建查询条件
    Criteria criteria = Criteria.where("age").lt(30);
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
}

6. $lte 小于等于

用于匹配字段值小于等于指定值的文档:

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

查询user集合中age小于等于30的文档:

@Test
public void findUser2() {
    // 创建查询条件
    Criteria criteria = Criteria.where("age").lte(30);
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
}

7. $in

在指定值数组中,用于匹配字段值在指定值数组中的文档:

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

查询user集合中name等于Alice或Jone或Jane的文档:

db.user.find({ name: { $in: ["Alice", "John", "Jane"] } })
@Test
public void findUser2() {
    // 创建查询条件
    Criteria criteria = Criteria.where("name").in(Arrays.asList("Alice","John","Jane"));
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
}

8. $nin

不在指定值数组中,用于匹配字段值不在指定值数组中的文档:

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

查询user集合中name不等于Alice、Jone、Jane的文档:

db.user.find({ name: { $nin: ["Alice", "John", "Jane"] } })
@Test
public void findUser2() {
    // 创建查询条件
    Criteria criteria = Criteria.where("name").nin(Arrays.asList("Alice","John","Jane"));
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}

3. MongoDB 逻辑查询操作符

逻辑操作符根据计算结果为 ture 或 false 的表达式来返回数据。MongoDB提供了丰富的逻辑查询操作符,用于在查询中进行逻辑运算和条件判断。以下是一些常用的逻辑查询操作符:

$and:用于同时满足多个条件的查询。
例如:db.collection.find({ $and: [ { condition1 }, { condition2 } ] })

$or:用于满足多个条件中的任意一个的查询。
例如:db.collection.find({ $or: [ { condition1 }, { condition2 } ] })

$not:用于否定一个条件的查询。
例如:db.collection.find({ field: { $not: { condition } } })

$nor:用于满足多个条件都不成立的查询。
例如:db.collection.find({ $nor: [ { condition1 }, { condition2 } ] })

这些逻辑查询操作符可以与其他查询条件结合使用,以实现更复杂的查询逻辑。

1. $and

用于同时满足多个条件的查询。

db.collection.find({ $and: [ { condition1 }, { condition2 } ] })

查询user集合中age大于等于18,并且name等于Alice的文档:

db.user.find({ $and: [ { age: { $gte: 18 } }, { name: "Alice" } ] })
@Test
public void findUser1() {
    // 创建查询条件:首先创建了一个Criteria对象,并使用andOperator方法来添加多个条件
    Criteria criteria = new Criteria();
    criteria.andOperator(
            Criteria.where("age").gte(18),
            Criteria.where("name").is("Alice")
    );
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
}
@Test
public void findUser1() {
    // 查询条件
    Criteria criteria =  Criteria.where("age").gte(18).and("name").is("Alice");
    // 查询对象
    Query query = new Query();
    query.addCriteria(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
}

2. $or

用于满足多个条件中的任意一个的查询。

db.collection.find({ $or: [ { condition1 }, { condition2 } ] })

查询user集合中age小于30,或者name等于Alice的文档:

db.user.find({ $or: [ { age: { gt: 30 } }, { name: "Alice" } ] })
@Test
public void findUser1() {
    // 创建查询条件
    Criteria criteria = new Criteria();
    // 首先创建了一个Criteria对象,并使用orOperator方法来添加多个条件
    criteria.orOperator(
        Criteria.where("age").gt(30),
        Criteria.where("name").is("Alice")
    );
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}

3. $not

用于否定一个条件的查询。

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

查询user集合中age小于30的文档:

db.user.find({ age: { $not: { $gte: 30 } } })
@Test
public void findUser1() {
    // 创建查询对象
    Query query = new Query(Criteria.where("age").not().gte(30));
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
}

4. $nor

用于满足多个条件都不成立的查询。

db.collection.find({ $nor: [ { condition1 }, { condition2 } ] })

查询user集合中age小于30,并且name不等于Alice的文档

db.users.find({ $nor: [ { age: { $gte: 30 } }, { name: "Alice" } ] })
@Test
public void findUser1() {
    Criteria criteria = new Criteria();
    criteria.norOperator(
            Criteria.where("age").gte(30),
            Criteria.where("name").is("Alice")
    );
    // 创建查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
}

4. 元素查询操作符

元素操作符根据字段是否存在或数据类型返回数据。

$exists:用于检查字段是否存在的查询。
例如:db.collection.find({ field: { $exists: true } })

$type:用于检查字段类型的查询。
例如:db.collection.find({ field: { $type: “string” } })

数据构造:向user集合中name等于Alice的文档新增一个字段

db.user.updateOne( 
    { name: "Alice" },
    {
      $set: {city: "ShangHai"}
    }
)
@Test
public void updateUser(){
    // 构建查询条件
    Criteria criteria = Criteria.where("name").is("Alice");
    Query query = Query.query(criteria);

    // 构建更新操作
    Update update = new Update();
    update.set("city", "ShangHai");

    // 执行更新操作
    mongoTemplate.updateMulti(query, update, "user");
}

在这里插入图片描述

1. $exists

用于检查字段是否存在的查询

db.collection.find({ field: { $exists: true } })

查询user集合中city字段是否存在:

db.user.find({ city: { $exists: true } })
@Test
public void findUser1() {
    // 查询条件
    Criteria criteria = Criteria.where("city").exists(true);
    // 查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);
    
    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
}

2. $type

用于检查字段类型的查询

db.collection.find({ field: { $type: "string" } })

$type 运算符接受的字段类型:

类型数值别名注意
双精度1“double”
字符串2“string”
对象3“object”
阵列4“array”
二进制数据5“binData”
ObjectId7“objectId”
布尔8“bool”
Date9“date”
null10“null”
正则表达式11“regex”
JavaScript13“javascript”
32 位整数16“int”
时间戳17“timestamp”
64 位整型18“long”
Decimal12819“decimal”
Min key-1“minKey”
Max key127“maxKey”

查询user集合中name的字段类型是否为string:

db.user.find({ name: { $type: "string" } })
db.user.find( { name : { $type : 2 } } ) 
@Test
public void findUser1() {
    // 查询条件
    Criteria criteria =  Criteria.where("name").type(2);
    // 查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0098, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music, cooking])
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}

5. MongoDB 数组查询操作符

数组操作符根据数组条件返回数据。MongoDB提供了多种数组查询操作符,用于在查询中对数组字段进行操作和匹配。以下是一些常用的数组查询操作符:

$elemMatch:用于在数组字段中匹配满足多个条件的元素的查询。
db.collection.find({ field: { $elemMatch: { condition1, condition2 } } })

$size:用于匹配数组字段的长度的查询。
db.collection.find({ field: { $size: 3 } })

$all:用于匹配数组字段中包含所有指定元素的查询。
db.collection.find({ field: { $all: [ element1, element2 ] } })

$in:用于匹配数组字段中包含指定元素的查询。例如:

db.collection.find({ field: { $in: [ element1, element2 ] } })
$nin:用于匹配数组字段中不包含指定元素的查询。例如:

db.collection.find({ field: { $nin: [ element1, element2 ] } })
$slice:用于返回数组字段的子集。例如:

db.collection.find({ field: { $slice: 5 } })
$addToSet:用于向数组字段添加唯一的元素。例如:

db.collection.updateOne({ _id: ObjectId(“…”) }, { $addToSet: { field: element } })
这些数组查询操作符可以与其他查询条件结合使用,以实现更复杂的查询逻辑。请注意,以上示例中的"collection"和"field"应替换为实际的集合名和字段名。

1. $elemMatch

用于在数组字段中匹配满足多个条件的元素的查询。

db.collection.find({ arrayField: { $elemMatch: { condition1, condition2 } } })
1.1 元素匹配

查询user集合中age大于等于30并且hobbies包含reading的文档:

db.user.find({ age: { $gte: 30 }, hobbies: { $elemMatch: { $eq: "reading" } } })
@Test
public void findUser1() {
    // 查询条件
    Criteria criteria =  Criteria.where("age").gte(30).and("hobbies").elemMatch(Criteria.where("$eq").is("reading"));
    // 查询对象
    Query query = new Query(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}
@Test
public void findUser1() {
    // 查询条件
    Criteria criteria =  new Criteria();
    criteria.andOperator(
            Criteria.where("age").gte(30),
            Criteria.where("hobbies").elemMatch(Criteria.where("$eq").is("reading"))
    );
    // 查询对象
    Query query = new Query();
    query.addCriteria(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    // User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}

查询user集合中hobbies数组包含reading的文档:

db.user.find( hobbies: { $elemMatch: { $eq: "reading" } } })
@Test
public void findUser1() {
    // 查询条件
    Criteria criteria =  Criteria.where("hobbies").elemMatch(Criteria.where("$eq").is("reading"));
    // 查询对象
    Query query = new Query();
    query.addCriteria(criteria);
    // 执行查询
    List<User> users = mongoTemplate.find(query, User.class);

    users.forEach(System.out::println);
    // User(id=668f53342e9dde5bccea0096, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
    //User(id=668f53342e9dde5bccea0097, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming, traveling])
    //User(id=668f53342e9dde5bccea0099, name=Mike, age=35, email=Mike@qq.com, hobbies=[reading, writing, painting])
}
1.2 嵌入式文档数组

① 查询survey集合中product"xyz"score大于或等于8 的文档:

db.survey.insertMany( [
   { "_id": 1, "results": [ { "product": "abc", "score": 10 },{ "product": "xyz", "score": 5 } ] },
   { "_id": 2, "results": [ { "product": "abc", "score": 8 },{ "product": "xyz", "score": 7 } ] },
   { "_id": 3, "results": [ { "product": "abc", "score": 7 },{ "product": "xyz", "score": 8 } ] },
   { "_id": 4, "results": [ { "product": "abc", "score": 7 },{ "product": "def", "score": 8 } ] },
   { "_id": 5, "results": { "product": "xyz", "score": 9 } }
] )

注意,_id5 的文档不包含数组。

db.survey.find( { results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } } )
@Data
@Document(collection = "survey")
public class Survey {
    @Id
    private int _id;
    private List<Result> results;
}

@Data
public class  Result {
    private String product;
    private int score;
}

@Test
public void findUser1() {
    // 查询条件
    Query query = new Query();
    query.addCriteria(Criteria.where("results").elemMatch(
        Criteria.where("product").is("xyz").and("score").gte(8)
    ));
    List<Survey> surveyList = mongoTemplate.find(query, Survey.class, "survey");

    surveyList.forEach(System.out::println);
    // Survey(_id=3, results=[Result(product=abc, score=7), Result(product=xyz, score=8)])
}

② 使用$elemMatch进行单一查询条件:

db.survey.find(
   { results: { $elemMatch: { product: "xyz" } } }
)

此查询会返回 results 中的任一 product"xyz" 的文档。

③ 不使用$elemMatch进行单一查询条件:

db.survey.find(
   { "results.product": "xyz" }
)

此查询结果还包括 _id5 的文档(不包含数组)

④ 查询student集合中所有数学成绩高于90分的学生:

db.student.insertMany( 
    [
      {"_id": 1,
			"name": "Alice",
			"grades": [
          { "subject": "Math", "score": 90 },
          { "subject": "English", "score": 85 },
          { "subject": "Science", "score": 95 }
        ]
      },
      {
        "_id": 2,
				"name": "Bob",
				"grades": [
          { "subject": "Math", "score": 80 },
          { "subject": "English", "score": 75 },
          { "subject": "Science", "score": 85 }
        ]
      },
      {
        "_id": 3, 
				"name": "Charlie",
				"grades": [
          { "subject": "Math", "score": 95 },
          { "subject": "English", "score": 90 },
          { "subject": "Science", "score": 92 }
        ]
      }
    ]
)
db.students.find({ grades: { $elemMatch: { subject: "Math", score: { $gt: 90 } } } })
@Data
@Document(collection = "student")
public class Student {
    @Id
    private int _id;
    private String name;
    private List<Grade> grades;
}

@Data
public class Grade {
    private String subject;
    private int score;
}

@Test
public void findUser1() {
    // 查询条件
    Query query = new Query();
    query.addCriteria(Criteria.where("grades").elemMatch(
        Criteria.where("subject").is("Math").and("score").gte(90)
    ));
    List<Student> studentList = mongoTemplate.find(query, Student.class, "student");

    studentList.forEach(System.out::println);
    // Student(_id=1, name=Alice, grades=[Grade(subject=Math, score=90), Grade(subject=English, score=85), Grade(subject=Science, score=95)])
    // Student(_id=3, name=Charlie, grades=[Grade(subject=Math, score=95), Grade(subject=English, score=90), Grade(subject=Science, score=92)])
}

2. $size

用于匹配数组字段的长度的查询。

db.collection.find({ arrayField: { $size: 3 } })

查询user集合中hobbies数组包含2个元素的所有文档:

db.user.insertMany([
  { name: "Alice", age: 25, email: "alice@example.com", hobbies: ["writing"] },
  { name: "John", age: 30, email: "John@qq.com", hobbies: ["reading", "gaming",] },
  { name: "Jane", age: 25, email: "Jane@qq.com", hobbies: ["sports", "music"] },
  { name: "Mike", age: 35, email: "Mike@qq.com", hobbies: ["reading", "writing", "painting", "cooking"] }
]);
db.user.find( { hobbies: { $size: 2 } } );
@Test
public void findUser1() {
    // 查询条件
    Query  query = new Query();
    Criteria criteria = Criteria.where("hobbies").size(2);
    query.addCriteria(criteria);

    List<User> users = mongoTemplate.find(query, User.class, "user");
    
    users.forEach(System.out::println);
    // User(id=66906798197700004d003dcd, name=John, age=30, email=John@qq.com, hobbies=[reading, gaming])
    //User(id=66906798197700004d003dce, name=Jane, age=25, email=Jane@qq.com, hobbies=[sports, music])
}

此查询返回 collectionfield 为包含 2 个元素的数组的所有文档。

3. $all

用于匹配数组字段中包含所有指定元素的查询。

db.collection.find({ field: { $all: [ element1, element2 ] } })
db.user.insertMany([
  { name: "Alice", age: 25, email: "alice@example.com", hobbies: ["reading", "writing", "music"] },
  { name: "John", age: 30, email: "John@qq.com", hobbies: ["reading", "gaming", "traveling"] },
  { name: "Jane", age: 25, email: "Jane@qq.com", hobbies: ["sports", "music", "cooking"] },
  { name: "Mike", age: 35, email: "Mike@qq.com", hobbies: ["reading", "writing", "painting"] }
]);

查询user集合中hobbies数组包含 “writing” 和 “music” 的文档:

db.user.find({ hobbies: { $all: [ "writing" , "music" ] } })
# 相当于
db.user.find({ $and: [ { hobbies: "writing" }, { hobbies: "music" } ] })
@Test
public void findUser1() {
    // 查询条件
    Query  query = new Query();
    Criteria criteria = Criteria.where("hobbies").all("music","writing");
    query.addCriteria(criteria);

    List<User> users = mongoTemplate.find(query, User.class, "user");

    users.forEach(System.out::println);
    // User(id=6690693d197700004d003dd0, name=Alice, age=25, email=alice@example.com, hobbies=[reading, writing, music])
}

4. 查询数组

4.1 匹配数组中的元素

构造测试数据:

db.students.insertMany( [
   { "_id" : 1, "grades" : [ 90, 80, 85 ] },
   { "_id" : 2, "grades" : [ 88, 90, 92 ] },
   { "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )

查询 grades 数组中包含 85 的文档:

db.students.find({ grades: 85 })
db.students.find({ grades: { $eq: 85 } })

查询结果:

// 1
{
    "_id": 1,
    "grades": [
        90,
        80,
        85
    ]
}

// 2
{
    "_id": 3,
    "grades": [
        85,
        100,
        90
    ]
}
4.2 精确匹配数组包含指定元素

查询 grades 数组同时包含90, 85元素的数组,而不考虑顺序或者数组中的其他元素,使用 $all 运算符:

db.students.find({ grades: { $all: [90, 85] } })
db.students.find({ $and: [ {grades: 90}, {grades: 85} ] })

查询结果:

// 1
{
    "_id": 1,
    "grades": [
        90,
        80,
        85
    ]
}

// 2
{
    "_id": 3,
    "grades": [
        85,
        100,
        90
    ]
}
4.3 匹配数组包含某个元素

构造测试数据:

db.students.insertMany( [
   { "_id" : 1, "grades" : [ 90, 80, 85 ] },
   { "_id" : 2, "grades" : [ 88, 90, 92 ] },
   { "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )

查询 grades 数组中包含 85 或者 80的文档:

db.students.find({ grades: { $in: [80,85] } })

查询结果:

// 1
{
    "_id": 1,
    "grades": [
        90,
        80,
        85
    ]
}

// 2
{
    "_id": 3,
    "grades": [
        85,
        100,
        90
    ]
}
4.4 精确匹配数组满足多个条件(使用$element)

$element 指定的多个指定条件必须同时满足

构造测试数据:

db.students.insertMany( [
   { "_id" : 1, "grades" : [ 90, 80, 85 ] },
   { "_id" : 2, "grades" : [ 88, 90, 92 ] },
   { "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )

查询 grades 数组满足 至少包含一个大于92且小于等于100 的条件 的文档:

db.students.find({ grades: { $elemMatch: { $gte: 92, $lte: 100 } } })

查询结果:

// 1
{
    "_id": 2,
    "grades": [
        88,
        90,
        92
    ]
}

// 2
{
    "_id": 3,
    "grades": [
        85,
        100,
        90
    ]
}
4.5 匹配数组满足多个条件(不使用$element)

① 构造测试数据:

db.student.insertMany( 
    [
      {
        "_id": 1,
		"name": "Alice",
		"grades": [
          { "subject": "Math", "score": 90 },
          { "subject": "English", "score": 85 },
          { "subject": "Science", "score": 95 }
        ]
      },
      {
        "_id": 2,
		"name": "Bob",
		"grades": [
          { "subject": "Math", "score": 80 },
          { "subject": "English", "score": 75 },
          { "subject": "Science", "score": 85 }
        ]
      },
      {
        "_id": 3, 
		"name": "Charlie",
		"grades": [
          { "subject": "Math", "score": 95 },
          { "subject": "English", "score": 90 },
          { "subject": "Science", "score": 92 }
        ]
      }
    ]
)

查询 grades 数组中的嵌套文档中包含 subject=Math 且 score>92 (不一定是同一个嵌套文档)的文档:

db.student.find({ "grades.subject": "Math", "grades.score": { $gt: 92} })

查询结果:

// grades数组的第1个文档中subject=Math,第3个文档中 score>92 ,所以满足条件
{
    "_id": 1,
    "name": "Alice",
    "grades": [
        {
            "subject": "Math",
            "score": 90
        },
        {
            "subject": "English",
            "score": 85
        },
        {
            "subject": "Science",
            "score": 95
        }
    ]
}

//  grades数组的第1个文档中subject=Math,且第1个文档中 score>92 ,所以也满足条件
{
    "_id": 3,
    "name": "Charlie",
    "grades": [
        {
            "subject": "Math",
            "score": 95
        },
        {
            "subject": "English",
            "score": 90
        },
        {
            "subject": "Science",
            "score": 92
        }
    ]
}

② 构造测试数据:

db.students.insertMany( [
   { "_id" : 1, "grades" : [ 90, 80, 85 ] },
   { "_id" : 2, "grades" : [ 88, 90, 92 ] },
   { "_id" : 3, "grades" : [ 85, 100, 90 ] }
] )

查询 grades 数组中的元素大于等于92并且小于等于100 的文档:

db.students.find({ grades: { $gte: 92, $lte: 100 } })

查询结果:

// 1
{
    "_id": 2,
    "grades": [
        88,
        90,
        92
    ]
}

// 2
{
    "_id": 3,
    "grades": [
        85,
        100,
        90
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我一直在流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值