做民宿加盟哪些网站比较好,海澜之家的网站建设目标,个人网站设计及实现论文,合肥仿站定制模板建站文章目录 1. 构造数据2. MongoDB 比较查询操作符1. $eq 等于1.1 等于指定值1.2 嵌入式文档中的字段等于某个值1.3 数组元素等于某个值1.4 数组元素等于数组值 2. $ne 不等于3. $gt 大于3.1 匹配文档字段3.2 根据嵌入式文档字段执行更新 4. $gte 大于等于5. $lt 小于6. $lte 小于… 文章目录 1. 构造数据2. MongoDB 比较查询操作符1. $eq 等于1.1 等于指定值1.2 嵌入式文档中的字段等于某个值1.3 数组元素等于某个值1.4 数组元素等于数组值 2. $ne 不等于3. $gt 大于3.1 匹配文档字段3.2 根据嵌入式文档字段执行更新 4. $gte 大于等于5. $lt 小于6. $lte 小于等于7. $in8. $nin 3. MongoDB 逻辑查询操作符1. $and2. $or3. $not4. $nor 4. 元素查询操作符1. $exists2. $type 5. MongoDB 数组查询操作符1. $elemMatch1.1 元素匹配1.2 嵌入式文档数组 2. $size3. $all 参考官方文档https://www.mongodb.com/zh-cn/docs/manual/reference/operator/query/and/ 1. 构造数据
① 批量插入4个文档到user集合
db.user.insertMany([{ name: Alice, age: 25, email: aliceexample.com, hobbies: [reading, writing, music] },{ name: John, age: 30, email: Johnqq.com, hobbies: [reading, gaming, traveling] },{ name: Jane, age: 25, email: Janeqq.com, hobbies: [sports, music, cooking] },{ name: Mike, age: 35, email: Mikeqq.com, hobbies: [reading, writing, painting] }
]);② SpringBoot整合MongoDB实现批量插入文档
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId
/dependencyAllArgsConstructor
NoArgsConstructor
Data
Document(collection user)
public class User {Idprivate String id;private String name;private Integer age;private String email;private ListString hobbies;public User(String name,Integer age,String email,ListString hobbies){this.name name;this.age age;this.email email;this.hobbies hobbies;}
}SpringBootTest
RunWith(SpringRunner.class)
public class BeanLoadServiceTest {Autowiredprivate MongoTemplate mongoTemplate;Testpublic void insertUser() {ListUser users Arrays.asList(new User(Alice, 25, aliceexample.com,Arrays.asList(reading, writing, music)),new User(John, 30, Johnqq.com, Arrays.asList(reading, gaming, traveling)),new User(Jane, 25, Janeqq.com, Arrays.asList(sports, music, cooking)),new User(Mike, 35, Mikeqq.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、 and、or、$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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println); // User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.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 {Idprivate String id;private Item item;private int qty;private ListObject tags;Datapublic 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);ListInventory inventoryList mongoTemplate.find(query, Inventory.class);inventoryList.forEach(System.out::println);// Inventory(id1.0, itemInventory.Item(nameab, code123), qty15, tags[A, B, C])
}1.3 数组元素等于某个值
查询 inventory 集合中 tags 数组包含值为 C 的元素的所有文档
db.inventory.find( { tags: { $eq: B } } )Test
public void findUser1() {// 创建查询条件Criteria criteria Criteria.where(tags).is(C);// 创建查询对象Query query new Query(criteria);ListInventory inventoryList mongoTemplate.find(query, Inventory.class);inventoryList.forEach(System.out::println);// Inventory(id1.0, itemInventory.Item(nameab, code123), qty15, tags[A, B, C])// Inventory(id5.0, itemInventory.Item(namemn, code000), qty20, 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);ListInventory inventoryList mongoTemplate.find(query, Inventory.class);inventoryList.forEach(System.out::println);// Inventory(id3.0, itemInventory.Item(nameij, code456), qty25, tags[A, B])// Inventory(id5.0, itemInventory.Item(namemn, code000), qty20, 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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.com, hobbies[sports, music, cooking])// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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 {Idprivate String id;private String item;private int quantity;private Carrier carrier;Datapublic 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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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));// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0098, nameJane, age25, emailJaneqq.com, hobbies[sports, music, cooking])// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])// User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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);// 执行查询ListUser users mongoTemplate.find(query, User.class);users.forEach(System.out::println);// User(id668f53342e9dde5bccea0096, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])//User(id668f53342e9dde5bccea0097, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming, traveling])//User(id668f53342e9dde5bccea0099, nameMike, age35, emailMikeqq.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 } }
] )注意_id 为 5 的文档不包含数组。
db.survey.find( { results: { $elemMatch: { product: xyz, score: { $gte: 8 } } } } )Data
Document(collection survey)
public class Survey {Idprivate int _id;private ListResult 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)));ListSurvey surveyList mongoTemplate.find(query, Survey.class, survey);surveyList.forEach(System.out::println);// Survey(_id3, results[Result(productabc, score7), Result(productxyz, score8)])
}② 使用$elemMatch进行单一查询条件
db.survey.find({ results: { $elemMatch: { product: xyz } } }
)此查询会返回 results 中的任一 product 为 xyz 的文档。
③ 不使用$elemMatch进行单一查询条件
db.survey.find({ results.product: xyz }
)此查询结果还包括 _id 为 5 的文档不包含数组
④ 查询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 {Idprivate int _id;private String name;private ListGrade 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)));ListStudent studentList mongoTemplate.find(query, Student.class, student);studentList.forEach(System.out::println);// Student(_id1, nameAlice, grades[Grade(subjectMath, score90), Grade(subjectEnglish, score85), Grade(subjectScience, score95)])// Student(_id3, nameCharlie, grades[Grade(subjectMath, score95), Grade(subjectEnglish, score90), Grade(subjectScience, score92)])
}2. $size
用于匹配数组字段的长度的查询。
db.collection.find({ arrayField: { $size: 3 } })查询user集合中hobbies数组包含2个元素的所有文档
db.user.insertMany([{ name: Alice, age: 25, email: aliceexample.com, hobbies: [writing] },{ name: John, age: 30, email: Johnqq.com, hobbies: [reading, gaming,] },{ name: Jane, age: 25, email: Janeqq.com, hobbies: [sports, music] },{ name: Mike, age: 35, email: Mikeqq.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);ListUser users mongoTemplate.find(query, User.class, user);users.forEach(System.out::println);// User(id66906798197700004d003dcd, nameJohn, age30, emailJohnqq.com, hobbies[reading, gaming])//User(id66906798197700004d003dce, nameJane, age25, emailJaneqq.com, hobbies[sports, music])
}此查询返回 collection 中 field 为包含 2 个元素的数组的所有文档。
3. $all
用于匹配数组字段中包含所有指定元素的查询。
db.collection.find({ field: { $all: [ element1, element2 ] } })db.user.insertMany([{ name: Alice, age: 25, email: aliceexample.com, hobbies: [reading, writing, music] },{ name: John, age: 30, email: Johnqq.com, hobbies: [reading, gaming, traveling] },{ name: Jane, age: 25, email: Janeqq.com, hobbies: [sports, music, cooking] },{ name: Mike, age: 35, email: Mikeqq.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);ListUser users mongoTemplate.find(query, User.class, user);users.forEach(System.out::println);// User(id6690693d197700004d003dd0, nameAlice, age25, emailaliceexample.com, hobbies[reading, writing, music])
}
文章转载自: http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.ftdlg.cn.gov.cn.ftdlg.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.tygn.cn.gov.cn.tygn.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.qmzwl.cn.gov.cn.qmzwl.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn