Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 具有唯一值的Mongodb筛选器结果_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 具有唯一值的Mongodb筛选器结果

Node.js 具有唯一值的Mongodb筛选器结果,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我收集了很多城市和邮政编码,其中城市的名称可以是相同的,而邮政编码是不同的 例如,我试图通过以“圣安东尼奥”开头的所有城市进行查询,并希望筛选结果,其中圣安东尼奥仅显示一个城市以及以“圣安东尼奥”开头的任何其他城市 以下是我使用的代码: Zipcodes.find( { city: { $regex: /San/, $options: 'i' } }, (err, result) => { if (err) { return res.send({ error:

我收集了很多城市和邮政编码,其中城市的名称可以是相同的,而邮政编码是不同的

例如,我试图通过以“圣安东尼奥”开头的所有城市进行查询,并希望筛选结果,其中圣安东尼奥仅显示一个城市以及以“圣安东尼奥”开头的任何其他城市

以下是我使用的代码:

Zipcodes.find(
  { city: { $regex: /San/, $options: 'i' } },
  (err, result) => {
    if (err) {
      return res.send({ error: err });
    }
    res.send({ data: result });
  },
).limit(20);
这给了我以下结果:

  {
        "loc": [
            -94.132581,
            31.515173
        ],
        "_id": "75972",
        "city": "SAN AUGUSTINE",
        "pop": 5916,
        "state": "TX"
    },
    {
        "loc": [
            -98.730929,
            31.162678
        ],
        "_id": "76877",
        "city": "SAN SABA",
        "pop": 4023,
        "state": "TX"
    },
    {
        "loc": [
            -100.481752,
            31.478165
        ],
        "_id": "76901",
        "city": "SAN ANGELO",
        "pop": 23800,
        "state": "TX"
    },
    {
        "loc": [
            -100.480036,
            31.419411
        ],
        "_id": "76904",
        "city": "SAN ANGELO",
        "pop": 25535,
        "state": "TX"
    },
    {
        "loc": [
            -100.390005,
            31.464738
        ],
        "_id": "76905",
        "city": "SAN ANGELO",
        "pop": 11284,
        "state": "TX"
    },
    {
        "loc": [
            -100.438586,
            31.470735
        ],
        "_id": "76903",
        "city": "SAN ANGELO",
        "pop": 32471,
        "state": "TX"
    },
    {
        "loc": [
            -95.034496,
            29.466033
        ],
        "_id": "77539",
        "city": "SAN LEON",
        "pop": 21905,
        "state": "TX"
    },
    {
        "loc": [
            -99.427148,
            27.062523
        ],
        "_id": "78067",
        "city": "SAN YGNACIO",
        "pop": 871,
        "state": "TX"
    },
    {
        "loc": [
            -98.460127,
            29.414799
        ],
        "_id": "78203",
        "city": "SAN ANTONIO",
        "pop": 7261,
        "state": "TX"
    },
    {
        "loc": [
            -98.525967,
            29.422855
        ],
        "_id": "78207",
        "city": "SAN ANTONIO",
        "pop": 58355,
        "state": "TX"
    },
    {
        "loc": [
            -98.5063,
            29.400217
        ],
        "_id": "78204",
        "city": "SAN ANTONIO",
        "pop": 11526,
        "state": "TX"
    },
    {
        "loc": [
            -98.479338,
            29.441338
        ],
        "_id": "78215",
        "city": "SAN ANTONIO",
        "pop": 1264,
        "state": "TX"
    },
    {
        "loc": [
            -98.545219,
            29.358366
        ],
        "_id": "78211",
        "city": "SAN ANTONIO",
        "pop": 30417,
        "state": "TX"
    },
    {
        "loc": [
            -98.492509,
            29.423711
        ],
        "_id": "78205",
        "city": "SAN ANTONIO",
        "pop": 1714,
        "state": "TX"
    },
    {
        "loc": [
            -98.497511,
            29.533387
        ],
        "_id": "78216",
        "city": "SAN ANTONIO",
        "pop": 30435,
        "state": "TX"
    },
    {
        "loc": [
            -98.419444,
            29.539525
        ],
        "_id": "78217",
        "city": "SAN ANTONIO",
        "pop": 27925,
        "state": "TX"
    }
它多次返回圣安东尼奥。我只需要一个

请帮助回答正确的问题。谢谢。

您可以使用+获得城市的唯一值:

db.collection.aggregate([
    {
        $match: { city: { $regex: "San", $options: "i" } }
    },
    {
        $group: {
            _id: "$city",
            doc: { $first: "$$ROOT" }
        }
    },
    {
        $replaceRoot: {
            newRoot: "$doc"
        }
    }
])

您可以使用+获得城市唯一的值:

db.collection.aggregate([
    {
        $match: { city: { $regex: "San", $options: "i" } }
    },
    {
        $group: {
            _id: "$city",
            doc: { $first: "$$ROOT" }
        }
    },
    {
        $replaceRoot: {
            newRoot: "$doc"
        }
    }
])

谢谢,这正是我需要的。-)谢谢你,这就是我需要的