使用 mongoose nodejs 过滤 mongodb 数据库

Posted

技术标签:

【中文标题】使用 mongoose nodejs 过滤 mongodb 数据库【英文标题】:Flter mongodb database using mongoose nodejs 【发布时间】:2020-12-08 21:11:18 【问题描述】:

我需要根据一些固定的标准过滤一些用户。我有一个 user 收藏和一个 talent 收藏。人才集合包含对主类别集合的引用。

我需要的是根据人才集合中的类别和用户集合中的一些键来过滤这些用户。

For example I need to search for a user whose gender is 'male' and education 'BTech' and will have talents as a programmer and tester

我的用户收藏是这样的,


    "_id": "5f1939239bd35429ac9cd78f",
    "isOtpVerified": "false",
    "role": "user",
    "adminApproved": 1,
    "status": 0,
    "languages": "Malayalam, Tamil, Telugu, Kannada",
    "name": "Test user",
    "email": "test@email.com",
    "phone": "1234567890",
    "otp": "480623",
    "uid": 100015,
    "bio": "Short description from user",
    "dob": "1951-09-07T00:00:00.000Z",
    "gender": "Male",
    "education": "Btech",
    "bodyType": "",
    "complexion": "",
    "height": "",
    "weight": "",
    "requests": [],
    "location": 
        "place": "place",
        "state": "state",
        "country": "country"
    ,
    "image": 
        "avatar": "5f1939239bd35429ac9cd78f_avatar.jpeg",
        "fullsize": "5f1939239bd35429ac9cd78f_fullsize.png",
        "head_shot": "5f1939239bd35429ac9cd78f_head_shot.jpeg",
        "left_profile": "5f1939239bd35429ac9cd78f_left_profile.png",
        "right_profile": "5f1939239bd35429ac9cd78f_right_profile.png"
    ,
    "__v": 42,
    "createdAt": "2020-07-23T07:15:47.387Z",
    "updatedAt": "2020-08-18T18:54:22.272Z",

人才集合

[
    
        "_id": "5f38efef179aca47a0089667",
        "userId": "5f1939239bd35429ac9cd78f",
        "level": "5",
        "chars": 
            "type": "Fresher",
        ,
        "category": "5f19357b50bcf9158c6be572",
        "media": [],
        "createdAt": "2020-08-16T08:35:59.692Z",
        "updatedAt": "2020-08-16T08:35:59.692Z",
        "__v": 0
    ,
    
        "_id": "5f3b7e6f7e322948ace30a2c",
        "userId": "5f1939239bd35429ac9cd78f",
        "level": "3",
        "chars": 
            "type": "Fresher",
        ,
        "category": "5f19359250bcf9158c6be573",
        "media": [
            
                "adminApproved": 0,
                "status": 0,
                "_id": "5f3c22573065f84a48e04a14",
                "file": "id=5f1939239bd35429ac9cd78f&dir=test&img=5f1939239bd35429ac9cd78f_image_undefined.jpeg",
                "description": "test",
                "fileType": "image",
                "caption": "test file"
            ,
            
                "adminApproved": 0,
                "status": 0,
                "_id": "5f3c2d7a8c7f8336b0bfced2",
                "file": "id=5f1939239bd35429ac9cd78f&dir=test&img=5f1939239bd35429ac9cd78f_image_1.jpeg",
                "description": "this is a demo poster for testing",
                "fileType": "image",
                "caption": "A Test Poster"
            
        ],
        "createdAt": "2020-08-18T07:08:31.532Z",
        "updatedAt": "2020-08-18T19:35:22.899Z",
        "__v": 2
    
]

上述文档中的类别是一个单独的类别。类别集合为,

[
        
            "_id": "5f19359250bcf9158c6be573",
            "status": true,
            "title": "Testing",
            "description": "Application tester",
            "code": "test",
            "characteristics": [],
            "createdAt": "2020-07-23T07:00:34.221Z",
            "updatedAt": "2020-07-23T07:00:34.221Z",
            "__v": 0
        ,
        
            "status": true,
            "_id": "5f29829a705b4e648c28bc88",
            "title": "Designer",
            "description": "UI UX Designer",
            "code": "uiux",
            "createdAt": "2020-08-04T15:45:30.125Z",
            "updatedAt": "2020-08-04T15:45:30.125Z",
            "__v": 0
        ,
        
            "_id": "5f19357b50bcf9158c6be572",
            "status": true,
            "title": "programming",
            "description": "Java programmer",
            "code": "program",
            "createdAt": "2020-07-23T07:00:11.137Z",
            "updatedAt": "2020-07-23T07:00:11.137Z",
            "__v": 0
        
    ]

所以我的过滤条件将是;


    categories: ["5f19359250bcf9158c6be573", "5f19357b50bcf9158c6be572"],
    minAge: 18,
    maxAge: 25,
    minHeight: 5,
    maxHeight: 6,
    minWeight: 50,
    maxWeight: 80,
    complexion: "white",
    gender: "male",

而预期的结果将是一个用户同时具备上述天赋和追随条件,


    users:  ..User details.. ,
    medias:  ...medias from the matching talents.. 

【问题讨论】:

您问的问题是否相同? Filter mongodb documents 删除了那个。需要一些改变。你能帮我解决这个问题吗? 您可以编辑该问题并删除该问题。 @turivishal 无法获得结果尝试查找和匹配。但似乎错了。 没有得到确切的问题,您能否正确描述,编辑并将内容放入您的问题中 1)收集一份文件 2)收集两份文件 3)该文件的预期结果和 4)您尝试过的内容到目前为止。 【参考方案1】:

如果有两个collections,您需要通过primary key_id 将它们与外部字段一起使用join,您可以使用$lookup$match 进行过滤。 Documentation

【讨论】:

要对此进行扩展 - 您将需要使用聚合。有几种写法,但总的来说,第一步将是提到的$lookup,将两个集合中的文档连接起来。然后 $match 过滤到您的标准。 感谢亚当的补充。作为 $lookup 和 help 聚合然后过滤你可以使用 $match 甚至可以使用 find。【参考方案2】:

你需要使用$lookup with pipeline,

$match 你匹配 category 的条件 $lookup加入users收藏 $match 用户集合字段的条件 $match 排除在条件中通过的条件中未找到与 users 匹配的文档
db.talents.aggregate([
  
    $match: 
      category:  $in: ["5f19359250bcf9158c6be573", "5f19357b50bcf9158c6be572"] 
    
  ,
  
    $lookup: 
      from: "users",
      as: "users",
      let:  userId: "$userId" ,
      pipeline: [
        
          $match: 
            $expr: 
              $and: [
                 $eq: ["$$userId", "$_id"] ,
                 $eq: ["$gender", "Male"] ,
                 $eq: ["$education", "Btech"] 
                // ... add you other match criteria here 
              ]
            
          
        
      ]
    
  ,
   $match:  users:  $ne: []   
])

Playground

【讨论】:

这会过滤数据,但问题在于,如果用户有两个才能,则用户将在列表中出现两次。

以上是关于使用 mongoose nodejs 过滤 mongodb 数据库的主要内容,如果未能解决你的问题,请参考以下文章

NodeJS/Express/Mongoose 出错 Can't set headers after they are sent

Node js, mongoose 对数据库中的数据进行排序

如何在 Express 和 Mongoose 的一个控制器 API 内发出多个“查找”请求?

nodejs中使用mongoose保存数据

nodejs中使用mongoose保存数据

MongoDB / Mongoose / nodejs 中的引用 - 并行化