本文介绍了如何“(WHERE)列=列"在蒙古?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 Mongo 做一些简单的事情,所以我希望用它做一些更高级的事情.在我需要这个之前效果很好:

I like Mongo for simple things so I was hoping to use it for something more advanced. And that worked fine until I needed this:

UPDATE tbl SET a = b WHERE c <> 0

a = b 部分是我无法弄清楚的.我试过 mongodb.org,但在那里找不到.我还查找了 WHERE a = b 但我也找不到.

The a = b part is what I can't figure out. I tried mongodb.org, but I can't find it there. I also looked for WHERE a = b but I can't find that either.

另一种方法是获取所有行而不是单独更新它们,但我不喜欢这样.它必须更简单.

An alternative is so fetch all rows and than update them individually, but I don't like that. It has to be simpler.

谢谢.

推荐答案

您要查看文档以进行更新.
http://www.mongodb.org/display/DOCS/Updating

You want to check the documentation for updating.
http://www.mongodb.org/display/DOCS/Updating

您的代码可能如下所示:
db.tbl.update( { c:{$ne:0}}, { $set: { a : b } } );

Your code might look like:
db.tbl.update( { c:{$ne:0}}, { $set: { a : b } } );

如果您需要复习高级查询(例如使用 $ne),请查看此处:
http://www.mongodb.org/display/DOCS/Advanced+Queries

If you need to brush up on advanced queries (e.g. using $ne), then check here:
http://www.mongodb.org/display/DOCS/Advanced+Queries


显然,您无法使用同一文档中的数据进行更新.
MongoDB:使用同一文档中的数据更新文档

EDIT 2(使用 map reduce 的解决方案):

var c = new Mongo();
var db = c.getDB('db')
var s = db.getCollection('s')
s.drop();
s.save({z:1,q:5});
s.save({z:11,q:55});

db.runCommand({
mapreduce:'s',
map:function(){
  var i = this._id; //we will emit with a unique key. _id in this case
  this._id=undefined; //strange things happen with merge if you leave the id in
  //update your document with access to all fields!
  this.z=this.q;

  emit(i,this);
},
query:{z:1},    //apply to only certain documents
out:{merge:'s'} //results get merged (overwrite themselves in collection)
});

//now take a look
s.find();

这篇关于如何“(WHERE)列=列"在蒙古?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:35