开发者

How to change field or update fast in MongoDB?

开发者 https://www.devze.com 2023-03-26 19:49 出处:网络
Document is this {title:\"here\", lat:123.4512, lng:36.3423, time:\"2011-01-02\"} i want chane Document like this

Document is this

{title:"here", lat:123.4512, lng:36.3423, time:"2011-01-02"}

i want chane Document like this

{title:"here", {latlng: {lat:123.4512, lng:36.3423}}, time:"2011-01-02"}

so i will try this by mongodb console.

db.coll.find().forEach(function(u){
  u.latlng = {"lat":u.lat, "lng":u.lng};
  db.coll.save(u);
  db.coll.update(u, {$unset:{"map_x1":1, "map_y1":1}});
})

but it's very slow T.T

i think another soluti开发者_运维技巧on

db.coll.find().forEach(function(u){
  db.coll.update(u, {{"latlng" : {"lat":u.lat, "lng":u.lng}}, $unset:{"lat":1, "lng":1}});
})

but i can't run it. because first solution is still running ....

do you have any fast solution like this job?


Your second query is broken since it'll overwrite the entire document, use $set :

db.coll.find().forEach(function(u){
  db.coll.update(u, {$set:{"latlng" : {"lat":u.lat, "lng":u.lng}}, $unset:{"lat":1, "lng":1}});
})

This'll be at least twice as fast as your original attempt since there you're updating the same document twice, which is not necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消