【问题标题】:Perform a search on main collection field and array of objects simultaneously同时对主集合字段和对象数组执行搜索
【发布时间】:2017-02-06 20:39:56
【问题描述】:

我的文档结构如下:

{
     "codeId" : 8.7628945723895E13, // long numeric value stored in scientific notation by Mongodb
    "problemName" : "Hardware Problem", 
    "problemErrorCode" : "97695686856", 
    "status" : "active", 
    "problemDescription" : "ghdsojgnhsdjgh sdojghsdjoghdghd i0dhgjodshgddsgsdsdfghsdfg", 
    "subProblems" : [
        {
            "codeId" : 8.76289457238896E14, 
            "problemName" : "Some problem", 
            "problemErrorCode" : "57790389503490249640", 
            "problemDescription" : "This is edited", 
            "status" : "active", 
            "_id" : ObjectId("589476eeae39b20b1c15535b")
        }, 
        ...
     ]
}

我有一个搜索字段,应该按codeId 搜索,它基本上在搜索字段中充当parentCodeID,如下所示

现在,除了parentIdCode,我还想搜索codeIdproblemCodeproblemNameproblemDescription

如何使用正则表达式搜索查询子模块,同时使用"$or" 子句等标记某些父字段以实现此目的?

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    你可以试试这样的。

     query = {
            '$or': [{
                "codeId":somevalue
            }, {
                "subProblems.codeId": {
                    "$regex": searchValue,
                    "$options": "i"
                }
            }, {
               //rest of sub modules fields
            }]
        };
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      相关资源
      最近更新 更多