簡體   English   中英

使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 在 Mongodb 中搜索兩個 Collections

[英]Searching Through Two Collections in Mongodb using Mongoose

我的 MongoDB 設置中有兩個 Collections

1.Customers
2.Agents

他們都有first_name屬性。

我想在 collections 上運行搜索以獲取其 first_name 與特定字符串匹配的客戶或代理。

如何使用 Mongoose 實現這一目標?

我已經嘗試過,但我認為不是最好的方法如下:

let customers = await Customer.find({first_name:"user supplied string"});
let agents = await Agent.find({first_name:"user supplied string"});

let results = [...customers, ...agents]; //But I don't think this is best practice.

任何見解將不勝感激。 謝謝你。

我不認為 mongoose 允許您同時搜索 collections。

如果您想查找名稱為“用戶提供的字符串”的所有客戶和代理,您的查詢方法似乎是正確的,根據文檔: https://mongoosejs.com/docs/api.html#model_Model.find

除非您有其他特定要求,否則名稱應包含且不僅等於“userstring”,否則結果將以某種方式排序。 或僅返回最近創建的條目。

也許分享為什么您認為這不是最佳實踐?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM