交换MongoDB数组中的值 [英] Swap the values in a MongoDB array

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

问题描述

我的MongoDb集合中有一个类似于"geo": [39.897867,-121.8909978]的数组.

I have an array like "geo": [39.897867,-121.8909978] in my MongoDb collection.

我需要做的是交换数组中存在的值.

What I need to do is to swap the values present inside the array.

请帮助我. 谢谢...

Please help me on this. Thank you...

推荐答案

您可以使用数组索引进行交换.首先找到它并交换它.

You can swap by using array index . First find it and swap it.

db.myCollection.find().forEach(function(doc){
        db.myCollection.update({_id: doc._id},
          {$set: {"geo.0": doc.geo[1], "geo.1": doc.geo[0]}})
    })

这篇关于交换MongoDB数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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