同时在主集合字段和对象数组上执行搜索

时间:2017-02-06 11:50:51

标签: mongodb mongoose mongodb-query

我的文档结构如下:

{
     "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,如下所示

enter image description here

现在,与parentIdCode一起,我想要搜索codeIdproblemCodeproblemNameproblemDescription

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

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。

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