如何“(WHERE)column = column"在蒙哥? [英] How to "(WHERE) column = column" in Mongo?

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

问题描述

我喜欢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:使用同一文档中的数据更新文档


Apparently you can't update with data from the same document.
MongoDB: Updating documents using data from the same document

编辑2(使用地图缩小的解决方案):

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)column = column"在蒙哥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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