有没有一种方法可以向Mongo-db中的特定字段添加权重年龄而无需编制索引和进行聚合?

时间:2019-08-11 06:46:48

标签: node.js mongodb mongodb-query

文档格式:

[
    {
      "_id": "",
      "productId": "prod003",
      "productName": "Adbrief Indemnity",
      "productIconUrl": "",
      "type": "",
      "productType": "",
      "businessLineId": "Bus005",
      "description": "",
      "benefit": [
      ],
      "feature": [
        "1. Dashboard interaktif",
        "2. Web analytics AdMedika"
      ],

      "mediaId": [
        "Med001",
        "Med002",
        "Med003",
        "Med004"
      ],
      "documentId": [
        "doc001",
        "doc008",
        "doc003"
      ],
      "createdAt": "2019-07-12T06:16:00.229Z",
      "updatedAt": "2019-07-12T06:16:00.229Z"
    },
{
      "_id": "",
      "productId": "prod004",
      "productName": "Cash management",
      "productIconUrl": "",
      "type": "",
      "productType": "",
      "businessLineId": "Bus005",
      "description": "",
      "benefit": [
      ],
      "feature": [
        "1. Dashboard interaktif",
        "2. Web analytics AdMedika"
      ],

      "mediaId": [
        "Med001",
        "Med002",
        "Med003",
        "Med004"
      ],
      "documentId": [
        "doc001",
        "doc008",
        "doc003"
      ],
      "createdAt": "2019-07-12T06:16:00.229Z",
      "updatedAt": "2019-07-12T06:16:00.229Z"
    }
]

在上述文档中,查找查询应根据优先级按以下顺序工作:

  • productName
  • 说明(最低)
  • 功能(最新)

我想在mongodb中搜索多个具有优先权的字段,

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

由于其短路行为,请尝试在聚合或查询级别使用$ or,并且可以将优先级顺序设置为$ or或在文档中首先搜索哪个参数

https://docs.mongodb.com/manual/reference/operator/aggregation/or/#behavior

在这里搜索是您要在数据库集合中找到的关键

$or:[
 {productName:{$regex:search,$options:'i'}},
 {description:{$regex:search,$options:'i'}},
 {feature:{{$in:[new RegExp(search)]}}}
]