MongoDB 中的反向数组字段 [英] Reverse array field in MongoDB
本文介绍了MongoDB 中的反向数组字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以错误顺序输入的位置字段的集合:
I have a collection with a location field that was entered in the wrong order:
location: [38.7633698, -121.2697997]
当我尝试使用 ...
db.collection.ensureIndex({'location': '2d'});
...我收到以下错误,因为纬度和经度颠倒了.
... I get the following error because the latitude and longitude are reversed.
"err" : "location object expected, location array not in correct format",
"code" : 13654
如何为 mongo shell 中的每个文档反转这个数组?
How can I reverse this array for each document in the mongo shell?
推荐答案
db.loc.find().forEach(function (doc) {
var loc = [ doc.location[1], doc.location[0] ];
db.loc.update(doc, { $set: { location: loc } });
})
这篇关于MongoDB 中的反向数组字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文