我有 mongo 中的屬性列表,我正在查詢一些嵌套字段。這是我的代碼,public List<Brand> searchBrands(Request request) { final MongoCollection<Document> collection = mongoDatabase.getCollection("shop"); final Document query = new Document(); final Document projection = new Document(); final List<Brand> brandList = new ArrayList<>(); query.append("_id", request.getId()); query.append("isActive", true); if (request.Year() != null) { query.append("attributes.name", "myYear"); query.append("attributes.value", request.getYear()); } projection.append("brand.code", 1.0); projection.append("brand.description", 1.0); projection.append("_id", 0.0); Block<Document> processBlock = document -> brandList. add(Brand.builder().code(document.get("brand",Document.class).getString("code")) .description(document.get("brand",Document.class).getString("description")) .build()); collection.find(query).projection(projection).forEach(processBlock); return brandList;}以上代碼正確返回結(jié)果,有 72 個具有相同品牌代碼的商品。但我想根據(jù)brand.code 獲取不同的內(nèi)容,我該怎么做?
1 回答

犯罪嫌疑人X
TA貢獻(xiàn)2080條經(jīng)驗 獲得超4個贊
我不確定您使用哪個 mongodb 客戶端庫來為 mongodb 創(chuàng)建查詢;我正在分享您可以在 mongodb 控制臺中運(yùn)行的查詢以獲得您想要的結(jié)果。我希望您知道如何使用 mongodb 客戶端庫創(chuàng)建此查詢
db.shop.distinct('brand.code', myQuery) //Replace myQuery with your query e.g. {isActive: true}
添加回答
舉報
0/150
提交
取消