簡體   English   中英

為mongoDB集合中的所有文檔添加2dsphere索引

[英]add 2dsphere index for all documents in mongoDB collection

我有一個以long / lat作為字段的mongoDB表。 我需要基於這些字段的值向集合添加2dsphere索引。 更新集合中所有文檔的最佳方法是什么?

有這樣的文件嗎?

> db.test.findOne()
{ "_id" : 0, "lat" : 45.2, "lon" : 37.5 }

在mongo shell中,嘗試此更新

> db.test.find().forEach(function(doc) {
    var geo = { "type" : "Point", "coordinates" : [doc.lon, doc.lat] }
    db.test.update({ "_id" : doc._id }, { "$set" : { "loc" : geo }, "$unset" : { "lat" : true, "lon" : true } })
})

現在創建地理位置索引:

> db.test.ensureIndex({ "loc" : "2dsphere" })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM