gpt4 book ai didi

java - 插入嵌入式文档而不阅读整个文档 - spring,mongo

转载 作者:行者123 更新时间:2023-12-01 08:53:27 24 4
gpt4 key购买 nike

我有这个 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);

}

然而,问题是产品是一个 大对象,它包含一组重属性 工厂可以有 2000 个产品 0x251819242134。

因此,虽然在此阶段不需要,但检索到的工厂会导致大量内存消耗。有没有办法在不读取整个对象的情况下将新产品对象直接附加到工厂文档中?

编辑:

至于评论,我试过:

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

所以我修改为在 push 之前将其转换为字符串:

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);
}

此推送对象正确,但在阅读时:
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);

};
}

}

关于java - 插入嵌入式文档而不阅读整个文档 - spring,mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62173077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com