第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

加入子數(shù)組中的多個(gè)記錄

加入子數(shù)組中的多個(gè)記錄

森林海 2023-06-08 14:50:12
我有一個(gè)員工記錄,其中包含僅包含 id 的 leads 數(shù)組,基本上是他領(lǐng)導(dǎo)的其他員工。我想輸出將每個(gè)潛在客戶與員工集合中的名稱連接起來(lái)的結(jié)果員工集合{    "_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")    ]}預(yù)期產(chǎn)出{    "_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問(wèn)題是,是否只有一個(gè)連接語(yǔ)句,或者我是否必須對(duì)數(shù)據(jù)庫(kù)進(jìn)行多次調(diào)用(天真方式)?
查看完整描述

1 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

您不必多次調(diào)用數(shù)據(jù)庫(kù),$lookup將完全按照您在聚合框架中的要求進(jìn)行操作。

嘗試以下查詢:

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

}


查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)