0

请参考这个问题这里

我有同样的问题,除了我没有提供任何地理数据。

下面是我的模型的地理数据实现部分

     startLocation: {
      // GeoJSON
      type: {
        type: String,
        default: 'Point',
        enum: ['Point']
      },
      coordinates: [Number],
      address: String,
      description: String
    },
    locations: [
      {
        type: {
          type: String,
          default: 'Point',
          enum: ['Point']
        },
        coordinates: [Number],
        address: String,
        description: String,
        day: Number
      }
    ]

以下每个案例的相同请求正文

{
    "name": "The Test Tourxx",
    "duration": 1,
    "maxGroupSize": 1,
    "difficulty": "medium",
    "price": 501,
    "summary": "Nothing exciting here man!",
    "imageCover": "tour-3-cover.jpg",
    "ratingsAverage": 4,
    "guides": ["5e85e24bd1b3f918dc07bb3d", "5e85e262d1b3f918dc07bb3e"]
}

下面连接本地MongoDB时的响应

{
    "status": "error",
    "error": {
        "driver": true,
        "name": "MongoError",
        "index": 0,
        "code": 16755,
        "errmsg": "Can't extract geo keys: { _id: ObjectId('5e85fc64a4031f1734b55515'), startLocation: { type: \"Point\", coordinates: [] }, ratingsAverage: 4, ratingsQuantity: 0, rating: 4.5, images: [], createdAt: new Date(1585839200315), startDates: [], secretTour: false, guides: [ ObjectId('5e85e24bd1b3f918dc07bb3d'), ObjectId('5e85e262d1b3f918dc07bb3e') ], name: \"The Test Tourxx\", duration: 1, maxGroupSize: 1, difficulty: \"medium\", price: 501, summary: \"Nothing exciting here man!\", imageCover: \"tour-3-cover.jpg\", locations: [], slug: \"the-test-tourxx\", __v: 0 }  Point must only contain numeric elements",
        "statusCode": 500,
        "status": "error"
    },
    "stack": "MongoError: Can't extract geo keys: { _id: ObjectId('5e85fc64a4031f1734b55515'), startLocation: { type: \"Point\", coordinates: [] }, ratingsAverage: 4, ratingsQuantity: 0, rating: 4.5, images: [], createdAt: new Date(1585839200315), startDates: [], secretTour: false, guides: [ ObjectId('5e85e24bd1b3f918dc07bb3d'), ObjectId('5e85e262d1b3f918dc07bb3e') ], name: \"The Test Tourxx\", duration: 1, maxGroupSize: 1, difficulty: \"medium\", price: 501, summary: \"Nothing exciting here man!\", imageCover: \"tour-3-cover.jpg\", locations: [], slug: \"the-test-tourxx\", __v: 0 }  Point must only contain numeric elements\n    at Function.create (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\core\\error.js:43:12)\n    at toError (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\utils.js:149:22)\n    at C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\operations\\common_functions.js:265:39\n    at handler (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\core\\sdam\\topology.js:913:24)\n    at C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\connection_pool.js:352:13\n    at handleOperationResult (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\core\\sdam\\server.js:487:5)\n    at MessageStream.messageHandler (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\connection.js:270:5)\n    at MessageStream.emit (events.js:311:20)\n    at processIncomingData (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\message_stream.js:144:12)\n    at MessageStream._write (C:\\Users\\Cody\\Desktop\\X-Files\\NodeJS\\4-natours\\node_modules\\mongodb\\lib\\cmap\\message_stream.js:42:5)\n    at doWrite (_stream_writable.js:441:12)\n    at writeOrBuffer (_stream_writable.js:425:5)\n    at MessageStream.Writable.write (_stream_writable.js:316:11)\n    at Socket.ondata (_stream_readable.js:714:22)\n    at Socket.emit (events.js:311:20)\n    at addChunk (_stream_readable.js:294:12)"
}

连接到下面的 MongoDB Atlas 时的响应(已删除无关药水)

{
    "status": "success",
    "data": {
        "tour": {
            "startLocation": {
                "type": "Point",
                "coordinates": []
            },
            "ratingsAverage": 4,
            "images": [],
            "guides": [
                "5e85e24bd1b3f918dc07bb3d",
                "5e85e262d1b3f918dc07bb3e"
            ],
            "_id": "5e85fb952754013728fc950c",
            "name": "The Test Tourxx",
            "duration": 1,
            "maxGroupSize": 1,
            "difficulty": "medium",
            "price": 501,
            "summary": "Nothing exciting here man!",
            "imageCover": "tour-3-cover.jpg",
            "locations": [],
            "__v": 0,
        }
    }
}

可以看出,本地数据库在为“位置”创建空数组时会引发错误,但 Atlas 做得很好。请帮我找出原因。

Mongoose:5.9.7 Mockgoose:未在项目中使用 MongoDB:4.2.3(本地和 Atlas 两者)

4

1 回答 1

1

我知道这不是生产代码,并且您正在处理少量数据,因此将您的 tours 集合放在 compass 中,然后运行脚本以再次导入数据。我不知道它为什么有效,但它有效。我猜这是旧索引的问题?我不知道,它对我有用。

因此,如果这对您不起作用,我猜您只是收到此错误,因为您将 startLocation 作为 2dsphere 索引,并且可能 mongo 要求您在此处输入数据。

尝试在下面创建游览

    {
    "name": "The test tttttttt",
    "duration": 4,
    "maxGroupSize": 10,
    "difficulty": "easy",
    "price": 1,
    "summary": "Exciting testt",
    "imageCover": "mimimi.png",
    "startLocation": {
    "description": "Aspen, USA",
    "type": "Point",
    "coordinates": [-106.822318, 39.190872],
    "address": "419 S Mill St, Aspen, CO 81611, USA"
    }
}

我所做的只是添加 startLocation 对象。

于 2020-07-08T12:19:57.800 回答