spring-mongo-template中对内嵌集合对象 不定参数的查询

测试数据:

{
  "_id" : ObjectId("54c752ed5c01557fc2eba8f1"),
  "title" : "测试1,27",
  "products" : [{
      "name" : "石油",
      "classid" : "001",
      "status" : "1",
      "isorg" : "Y",
      "_class" : "jyd.info.article.model.Customization"
    }],
}

/* 2 */
{
  "_id" : ObjectId("54c755f34c2815a1467c5391"),
  "title" : "测试多产品",
  "products" : [{
      "name" : "nn油",
      "classid" : "001002",
      "status" : "1",
      "isorg" : "Y",
      "_class" : "jyd.info.article.model.Customization"
    }, {
      "name" : "xx油",
      "classid" : "001003",
      "status" : "1",
      "isorg" : "Y",
      "_class" : "jyd.info.article.model.Customization"
    }, {
      "name" : "ss油",
      "classid" : "001",
      "status" : "1",
      "isorg" : "Y",
      "_class" : "jyd.info.article.model.Customization"
    }]
}

/* 3 */
{
  "_id" : ObjectId("54c758664c28d74b28d8dc67"),
  "_class" : "jyd.info.article.model.Article",
  "title" : "test3",
  "products" : [{
      "name" : "eee",
      "classid" : "001005",
      "status" : "1",
      "isorg" : "Y",
      "_class" : "jyd.info.article.model.Customization"
    }]
}
需要的接口:

List<Article> selectByParas(String[] products,String columnId);

接口实现:

	@Override
	public List<Article> selectByParas(String[] products, String columnId) {
		Query query = new Query();
		Criteria c = new Criteria();
		Criteria[] c1 = new Criteria[products.length+1];
		for(int i=0;i<products.length;i++){
			c1[i] = Criteria.where("products.classid").is(products[i]);
		}
		c1[products.length] = Criteria.where("columnId").is(columnId);
		c.andOperator(c1);
		query.addCriteria(c);
		return mongoTemplate.find(query, Article.class);
	}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Data MongoDB ,`MongoWriter` 接口用于将对象转换为 MongoDB 文档并插入到集合。`doInsertBatch` 方法是 `MongoTemplate` 类的一个方法,用于批量插入数据。 下面是一个简单的示例,展示了如何使用 `MongoWriter` 和 `doInsertBatch` 方法将对象批量插入到 MongoDB 集合: ```java public class Person { private String name; private int age; // getters and setters } public class PersonWriter implements MongoWriter<Person> { @Override public Document toDocument(Person object, MongoConverter converter) { Document document = new Document(); document.put("name", object.getName()); document.put("age", object.getAge()); return document; } } public class PersonDao { private MongoTemplate mongoTemplate; public void insertBatch(List<Person> persons) { mongoTemplate.execute(Person.class, collection -> { List<Document> documents = new ArrayList<>(); for (Person person : persons) { MongoWriter<Person> writer = new PersonWriter(); Document document = writer.toDocument(person, mongoTemplate.getConverter()); documents.add(document); } collection.insertMany(documents); return null; }); } } ``` 在上面的示例,`PersonWriter` 类实现了 `MongoWriter` 接口,并重写了 `toDocument` 方法,将 `Person` 对象转换为 MongoDB 文档。`PersonDao` 类的 `insertBatch` 方法使用 `MongoTemplate` 的 `execute` 方法执行 MongoDB 操作。在该方法内部,首先将传入的 `Person` 对象列表转换为 MongoDB 文档,并将这些文档存储在 `documents` 列表。最后,使用 `collection.insertMany` 方法将文档批量插入到 MongoDB 集合

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值