gpt4 book ai didi

mongodb - MongoDB 中的反向数组字段

转载 作者:可可西里 更新时间:2023-11-01 09:10:55 25 4
gpt4 key购买 nike

我有一个位置字段输入顺序错误的集合:

location: [38.7633698, -121.2697997]

当我尝试使用...在字段上放置一个二维索引时

db.collection.ensureIndex({'location': '2d'});

...我收到以下错误,因为纬度和经度颠倒了。

"err" : "location object expected, location array not in correct format",
"code" : 13654

如何在 mongo shell 中为每个文档反转此数组?

最佳答案

db.loc.find().forEach(function (doc) {
var loc = [ doc.location[1], doc.location[0] ];
db.loc.update(doc, { $set: { location: loc } });
})

关于mongodb - MongoDB 中的反向数组字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com