亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

加入子數組中的多個記錄

加入子數組中的多個記錄

森林海 2023-06-08 14:50:12
我有一個員工記錄,其中包含僅包含 id 的 leads 數組,基本上是他領導的其他員工。我想輸出將每個潛在客戶與員工集合中的名稱連接起來的結果員工集合{    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"),    "name" : "John"}{    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"),    "name" : "Jane"}{    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0b4"),    "name" : "Richard"}employee_leads集合{    "_id" : ObjectId("5d55ac30e533bc76e4581923"),    "employee_id" : ObjectId("5d4dc8635dd32dbcba4ae0c5"),    "leads" : [         ObjectId("5d4dc8635dd32dbcba4ae0ba"),         ObjectId("5d4dc8635dd32dbcba4ae0bb")    ]}預期產出{    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0c3"),    "leads" : [         {            "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"),            "name" : "John"        },         {            "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"),            "name" : "Jane"        }    ]}試圖:Document match = new Document("$match", new BasicDBObject("_id", new ObjectId("5d55ac30e533bc76e4581923")));Document lookup = new Document("$lookup", new BasicDBObject("from", "employee"))        .append("localField", "leads")        .append("foreignField", "_id")        .append("as", "leads");// Document unwind = new Document("$unwind", "$leads");Document project = new Document("$project", new BasicDBObject("name", "$lead.name"));Document document = database.getCollection("employee_lead")        .aggregate(Arrays.asList(match, lookup, unwind, project))        .first();// TODO: iterate through the lead array and display it問題是,是否只有一個連接語句,或者我是否必須對數據庫進行多次調用(天真方式)?
查看完整描述

1 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

您不必多次調用數據庫,$lookup將完全按照您在聚合框架中的要求進行操作。

嘗試以下查詢:

db.getCollection("employee_leads").aggregate([

{

? ? ? ? $match : {

? ? ? ? ? ? "_id" : new ObjectId("5d55ac30e533bc76e4581923") // This is in case you want to filter anything.?

? ? ? ? }

},

{

? ? ? ? $lookup : {

? ? ? ? ? ? "from": "employee",

? ? ? ? ? ? "localField": "leads",

? ? ? ? ? ? "foreignField": "_id",

? ? ? ? ? ? "as": "leads"

? ? ? ? }

}])

上述查詢的 Java 等效代碼:


示例 1


List<Bson> aggregates = Arrays.asList(

? ? ? ? ? ? ? ? Aggregates.match(Filters.eq("_id", new ObjectId("5d55ac30e533bc76e4581923"))),

? ? ? ? ? ? ? ? Aggregates.lookup("employee", "leads", "_id", "leads"));

? ? ? ? AggregateIterable<Document> iterable = this.collection.aggregate(aggregates);

示例 2


List<Document> aggregates = Arrays.asList(

? ? ? ? new Document("$match", new Document("_id", new ObjectId("5d55ac30e533bc76e4581923"))),

? ? ? ? new Document("$lookup", new Document("from", "employee")

? ? ? ? .append("localField", "leads")

? ? ? ? .append("foreignField", "_id")

? ? ? ? .append("as", "leads")));


AggregateIterable<Document> iterable = collection.aggregate(aggregates);


for (Document row : iterable) {

? ? System.out.println(row.toJson());

}


查看完整回答
反對 回復 2023-06-08
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號