Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

nodejs mongodb查询数据库的时候能实现ES6中的filter吗?

例如我要查询: [1,2,3,4,5].filter(i=>i>2)

数据库 class

{ "_id" : 1, index:1 , "name" : "高二(1)班", classId:"a1"}
{ "_id" : 2, index:2 ,"name" : "高二(2)班", classId:"a2"}
{ "_id" : 3, index:3 ,"name" : "高二(3)班", classId:"a3"}

查询

const class = await class.find()  //正常查询数据库

// 只返回id大于2的班级列表
class.find().filter(doc=>doc.index > 2) 

// 或者经过很复杂的计算决定是否返回这一项
class.find().filter(doc=>{
    const list = [2,5,7,8,9,12] 
    return list.includes(doc.index)
}) 

需求:
1、希望查询数据库的时候能自定义过滤条件就相当于筛选数组一样。
2、能够支持分页的。
3、不是说把所有数据查询出来在用js进行筛选。
4、不是说用$gt、$lte、$in这种,因为我想在filter里面跨数据库去查询数据,然后比较是否满足条件

因为没办法跨数据库去关联并查询就想着是否有这种方法实现。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...