Mongo查询字段,不返回任何重复项

时间:2019-11-27 06:55:52

标签: node.js mongodb

我们如何在mongo中查询可以过滤字段的字段,以使任何字段都不会重复?正如您在“制作”下面的结果中看到的那样:“福特”返回了3次。

查询

Vehicle.model.find(query,{'Year':1,'Make':1,'Model':1})。exec(function(err,vehicle){     if(err ||!vehicle){         返回res.json({             讯息:错误         })     }

return res.status(200).send({
    message: "success",
    data: vehicle,
    totalVehicle: vehicle.length,
})

})

采样结果

{
    "message": "success",
    "data": [
        {
            "_id": "5dde068b8911713924be68e7",
            "Make": "Ford",
            "Model": "Fusion",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be68f6",
            "Make": "Chevrolet",
            "Model": "Silverado 1500",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be6901",
            "Make": "Nissan",
            "Model": "Sentra",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be6908",
            "Make": "Ford",
            "Model": "Fusion",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be690b",
            "Make": "Ford",
            "Model": "F-150",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be690c",
            "Make": "Ford",
            "Model": "Super Duty F-250 SRW",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be690e",
            "Make": "Kia",
            "Model": "Sorento",
            "Year": 2017
        },
        {
            "_id": "5dde068b8911713924be6913",
            "Make": "Dodge",
            "Model": "Grand Caravan",
            "Year": 2017
        }
    ],
    "totalVehicle": 8
}

2 个答案:

答案 0 :(得分:1)

您必须在此处使用aggregate来获取'Make'的所有唯一记录

  

例如:

 Vehicle.model.aggregate([
    { "$match": query }, // your query here
    { "$group": { _id: "$Make", "doc":{"$first":"$$ROOT"}} }, // grouping with 'Make'
    { "$replaceRoot":{"newRoot":"$doc"} },
    { '$project': {'Year': 1, 'Make': 1, 'Model': 1} }  // filter fields
 ]).exec((err, data) => {
    if (err) // handle error;
    console.log(data);
})

答案 1 :(得分:1)

如果使用版本3.4 and >,这将是您的正确查询:

YourModel.aggregate([
  {
    "$group": {
      _id: {
        "Make": "$Make",
        "Model": "$Model",
        "Year": "$Year"
      },
      "doc": {
        "$first": "$$ROOT"
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$doc"
    }
  },
  {
    "$project": {
      "Year": 1,
      "Make": 1,
      "Model": 1
    }
  }
]).exec((err, car) => /* do whatever you need */ )

如果您只需要返回Ford

var filterQuery = {Make: "Ford"} // or {Year: {$in: ["2017", "2018"]}} or any other query


 YourModel.aggregate([
      {
        $match: filterQuery
      },
      {
        "$group": {
          _id: {
            "Make": "$Make",
            "Model": "$Model",
            "Year": "$Year"
          },
          "doc": {
            "$first": "$$ROOT"
          }
        }
      },
      {
        "$replaceRoot": {
          "newRoot": "$doc"
        }
      },
      {
        "$project": {
          "Year": 1,
          "Make": 1,
          "Model": 1
        }
      }
    ]).exec((err, car) => /* do whatever you need */ )