在多个收藏夹中搜索猫鼬

时间:2019-03-12 04:44:59

标签: node.js database mongodb mongoose data-structures

我大约有约15个馆藏(具有不同数据结构的不同提供程序),这些馆藏有几个共同点,例如标题,描述和价格

我目前正在尝试使用公共字段为我的API实现搜索功能,并且能够针对每个集合单独进行搜索。

是否可以使用该公共字段一次查询所有15个馆藏? 一步一步地做的问题是性能问题(我必须依靠我的结果)以及在其之上有分页的事实。

我想我想用公共字段创建共享集合有点晚了。

2 个答案:

答案 0 :(得分:0)

无法对多个集合运行单个查询,您仍然可以做的是,并行运行所有查询,等待所有结果返回,然后可以在响应中发送集合结果。 / p>

代码应如下所示:

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
})

答案 1 :(得分:0)

根据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"
    }
}
])