I'm a beginnen with mongo and node.js, and in a existing project we have a collection with shops with geolocs. I get the following error (I have removed some fields)
"code" : 16755,
"errmsg" : "Can't extract geo keys: { _id: ObjectId('566990eea9c7a38740a305a3'),
id: 50, guid: \"NL7a09b334-7524-102d-a4ef-00163e5faa0c\", version: 0, owner: 118,
published: 1, loc: [ -9999, -9999 ], logo: \"69db95d0-d58d-40cf-80d3-ac80b8c86af8.png\" }
can't project geometry into spherical CRS: [ -9999, -9999 ]"
when I try to $set
some values on the shop. I see that the values -9999, -9999 are not correct but when I try to replace that with (in Robomongo) other values I get the same error. How can I fix this error, so I can edit the values in loc
and do a $set
?
You are correct that the error is because of the invalid geo coordinates you have in that document (see 2dsphere indexes ).
Assuming this is the document you have:
> db.test.find()
{
"_id": ObjectId("566990eea9c7a38740a305a3"),
"id": 50,
"guid": "NL7a09b334-7524-102d-a4ef-00163e5faa0c",
"version": 0,
"owner": 118,
"published": 1,
"loc": [
-9999,
-9999
],
"logo": "69db95d0-d58d-40cf-80d3-ac80b8c86af8.png"
}
The easiest method is to use the mongo
shell to perform an update , by using the ObjectId
and do a $set
on the loc
field . For example, by setting the loc
field to a valid coordinate (eg [-73.97, 40.77]):
> db.test.update({"_id": ObjectId("566990eea9c7a38740a305a3")}, {$set:{loc:[-73.97, 40.77]}})
Updated 1 existing record(s) in 1ms
WriteResult({
"nMatched": 1,
"nUpserted": 0,
"nModified": 1
})
Note that in the example above, I'm using the _id
field, which is guaranteed to be unique, so that the update command will only update that exact document. After that, your 2dsphere index creation should succeed:
> db.test.createIndex({loc:'2dsphere'})
{
"createdCollectionAutomatically": false,
"numIndexesBefore": 1,
"numIndexesAfter": 2,
"ok": 1
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.