MongoDB中的反向数组字段 [英] Reverse array field in MongoDB

查看:120
本文介绍了MongoDB中的反向数组字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a collection with a location field that was entered in the wrong order:

location: [38.7633698, -121.2697997]

当我尝试使用...在字段上放置2d索引时

When I try to place a 2d index on the field using ...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆