[英]Insert embeded document without reading whole document - spring, mongo
我有這個factory
收藏:
@Document(collection = "factory")
public class Factory
{
Private List<Product> products;
}
將Product
嵌入為產品。 當我必須向現有工廠添加產品時:
@Autowired
private FactoryRepository factoryRepository;
public void addProduct(Long id, Product product) {
Factory f = factoryRepository.findById(id);
f.addProduct(product);
factoryRepository.save(f);
}
然而,問題是產品是一個大的 object 包含一組重屬性,工廠可以有 2000 個產品。
因此,回收的工廠會導致大量 memory 消耗,盡管在此階段不需要。 有沒有辦法把append 一個新產品object 直接進廠文件不用讀整個object?
編輯:
至於評論,我試過:
public void addProduct(Long id, Product product) {
Document find = new Document("_id",id);
Document listItem = new Document("products",product);
Document push = new Document("$push", listItem);
collection.updateOne(find,push);
}
這給出了錯誤:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class product
所以我修改為在推送之前將其轉換為字符串:
public void addProduct(Long id, Product product) {
Document find = new Document("_id",id);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
Document listItem = new Document("products",ow.writeValueAsString(product));
Document push = new Document("$push", listItem);
collection.updateOne(find,push);
}
這正確地推動了 object 但在閱讀時:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [Product]
不過,我在這里無處可去。 有關解決此問題的任何想法?
您應該使用 MongoTemplate 通過推送更新產品以添加到現有產品。 就像是
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
@SpringBootApplication
public class So62173077Application {
public static void main(String[] args) {
SpringApplication.run(So62173077Application.class, args);
}
@Autowired
private MongoTemplate mongoTemplate;
@Document(collection = "factory")
public class Factory
{
private Long id;
private List<Product> products;
}
public Long createFactory() {
Factory factory = new Factory();
factory.id = 1L;
return mongoTemplate.insert(factory).id;
}
public void addProduct(Long id) {
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
Update update = new Update();
Product product = new Product();
product.name = "stackoverflow";
update.push("products", product);
mongoTemplate.updateFirst(query, update, Factory.class);
}
private class Product {
private String name;
}
@Bean
public ApplicationRunner runner() {
return args -> {
//Long id = createFactory();
addProduct(1L);
};
}
}
MongoDB 可能不鼓勵使用大型數組(獲取、原子操作、復雜查詢)。
根據您的需要,但我建議您在全新的“產品”集合中處理產品。 您當前的問題也將得到解決。
問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.