我將 Mongo DB Atlas 與 node.js 和 oboe 結合使用(oboe 將結果流式傳輸到 html)。我對這一切都很陌生,剛剛學習所有這些技術,所以用更簡單的術語解釋將不勝感激。目標是在兩個集合之間進行 $lookup,在本例中是“review”到“place”的集合。我研究過類似的答案,但要么不起作用,要么使用字符串,而不是 ObjectId。這很簡單,只需連接兩個集合的 ObjectId,但在使用 oboe 時,我無法從“左連接”的“地點”集合中提取數據(請參閱底部的 oboe 代碼,FWIW)。這是兩個集合中的文檔,然后是代碼。我究竟做錯了什么?我嘗試將它們轉換為字符串并與 .toString() 和 .str 連接,并為 localField 和foreignField 添加“place_id.ObjectId”和“_id.ObjectId”。另一件事是,我如何才能看到光標中的內容以了解我得到的內容?debug(cursor.ToArray()) 不起作用。提前致謝。review({ "_id": { "$oid": "5fd27fd9647f7bb815c4c946" }, "place_id": { "$oid": "5fbc37c4fc13ae680b00002b" }, // connect this... "user_id": { "$oid": "5fbc10ecfc13ae232d000068" }, "title": "Black Forest -- unforgettable!", "description": "The forest was great.", "score": { "$numberInt": "5" }place{ "_id": { "$oid": "5fbc37c4fc13ae680b00002b" }, // connected to _id above "name": "Black Forest (Schwarzwald)", "category": "activity", "city": "Freiburg", "country": "Germany", "description": "The Black Forest (German: Schwarzwald [??va?tsvalt] (About this soundlisten)) is a large forested mountain range.]", "image": { "filename": "1607629020164_black_forest.jpg", "mime": "image/jpeg" }, "state": ""})router.get('/', async (req, res, next) => { debug('get all reviews api'); try { const q = req.query.q; const collation = { locale: 'en_US', strength: 1 }; const matchStage = {}; if (q) { matchStage.$text = { $search: q }; } const pipeline = [ { $match: matchStage, }, { $lookup: { from: 'place', localField: 'place_id', foreignField: '_id', as: 'place', }, }, ];光標轉到雙簧管并成為“項目”。我希望使用模板字符串,例如{item.place.name}在將其放入 html 時獲取數據。這就是我訪問它的方式,對嗎?
兩個集合的 ObjectId 的簡單 $lookup“左連接”在 Mongo DB 中不起作用
四季花海
2023-11-02 20:06:42