簡體   English   中英

在多個收藏夾中搜索貓鼬

[英]Search in multiple collections mongoose

我大約有約15個館藏(具有不同數據結構的不同提供程序),它們具有一些共同點,例如標題,描述和價格

我目前正在嘗試使用公共字段為我的API實現搜索功能,並且能夠針對每個集合單獨進行搜索。

是否可以使用該公共字段一次查詢所有15個集合? 一步一步地做的問題是性能問題(我必須精簡我的結果)以及在它上面有分頁的事實。

我想我想創建一個與公共字段共享的集合有點晚了。

無法對多個集合運行單個查詢,您仍然可以做的是,並行運行所有查詢並等待所有結果返回,然后可以在響應中發送集合結果。

代碼應如下所示:

var promises = [];
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());

Promise.all(promises).then(results=>{
    // results[0] will have docs of first query
    // results[1] will have docs of second query
    // and so on...

    // you can combine all the results here and send back in response
}).catch(err=>{
    //handle error here
})

根據mongo文檔,$ lookup只能加入一個外部集合。

您可以做的是將userInfo和userRole組合到一個集合中,如所提供的示例是基於關系數據庫架構的。 Mongo是noSQL數據庫-這就需要不同的文檔管理方法。

請在下面的兩步查詢中找到,該查詢將userInfo和userRole結合在一起-創建用於上一次查詢的新臨時集合以顯示組合數據。 在上一個查詢中,有一個選項可以使用$ out並使用合並的數據創建新的集合以供以后使用。

創建收藏

db.sivaUser.insert(
{    
"_id" : ObjectId("5684f3c454b1fd6926c324fd"),
    "email" : "admin@gmail.com",
    "userId" : "AD",
    "userName" : "admin"
})

 //"userinfo"
db.sivaUserInfo.insert(
{
"_id" : ObjectId("56d82612b63f1c31cf906003"),
"userId" : "AD",
"phone" : "0000000000"
})

//"userrole"
db.sivaUserRole.insert(
{
"_id" : ObjectId("56d82612b63f1c31cf906003"),
"userId" : "AD",
"role" : "admin"
})

集合收集

db.sivaUserInfo.aggregate([
{$lookup:
    {
       from: "sivaUserRole",
       localField: "userId",
       foreignField: "userId",
       as: "userRole"
    }
},
{
    $unwind:"$userRole"
},
{
    $project:{
        "_id":1,
        "userId" : 1,
        "phone" : 1,
        "role" :"$userRole.role"
    }
},
{
    $out:"sivaUserTmp"
}
])

db.sivaUserTmp.aggregate([
{$lookup:
    {
       from: "sivaUser",
       localField: "userId",
       foreignField: "userId",
       as: "user"
    }
},
{
    $unwind:"$user"
},
{
    $project:{
        "_id":1,
        "userId" : 1,
        "phone" : 1,
        "role" :1,
        "email" : "$user.email",
        "userName" : "$user.userName"
    }
}
])

暫無
暫無

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

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