所以我的 Node.JS 項目中有以下模型:const Activity = mongoose.Schema({ name: { type: String, require: [true, "A activity must have a name"] }, city: { type: String, require: [true, "A location must have a city"] }, country: { type: String, require: [true, "A location must have a county"] },}); (為了方便起見,我省略了一些屬性,_id 是 Mongo 自動生成的)我現在想要實現的是獲取所有不同位置的列表。所以基本上在 SQL 中: SELECT city,country FROM Activity WHERE city=regEx OR country=regEx;到目前為止我嘗試了什么:let result = await Activity.find({$or: [{city: regEx}, {region: regEx}, {country: regEx}]}, "region city country -_id");result = fastSet(result);... 其中 fastSet() 是一個刪除重復項的函數。但現在我想在一個查詢中做到這一點。到目前為止,我發現我可以將 aggregate() 與 $match (和 $or 內部)一起使用,然后可能是 group 和 project 但我想如果我在這里使用 group ,我不會得到預期的結果。正確結果的一個例子是:let result = [{city:"City A", country:"Country A"}, {city:"City B", country:"Country A"}, {city:"City C", country:"Country C"}];...等等。我該如何做到這一點?
如何在特定條件下使用貓鼬獲得不同的對象?
慕標琳琳
2022-10-27 14:25:34