您想检查文档以进行更新。
http://www.mongodb.org/display/DOCS/Updating
您的代码可能是这样的:
db.tbl.update({ c:{$ne:0}}, { $set: { a : b } });
如果你需要刷上先进的查询(例如,使用$ne
),然后点击这里:
http://www.mongodb.org/display/DOCS/Advanced+Queries
编辑:
显然,你不能用同一文件的数据进行更新。
MongoDB: Updating documents using data from the same document
EDIT 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();
来源
2011-11-22 17:07:53
z5h
的'$ ne'是没问题的。 'a = b'是。您将值“b”分配给列“a”。我想将列“b”的值赋给列“a”。不是'SET a ='b'',而是'SET a = b' – Rudie
我编辑了我的答案。显然'update'不能引用当前文档。我认为可以用map-reduce来完成。致力于解决方案发布... – z5h
@Rudie:使用地图/缩小解决方案再次更新。 – z5h