如何迭代MongoD集合并一次更新1条记录? [英] How do I iterate a MongoD collection and update 1 record at a time?
问题描述
我是Mongo的新手。我需要在查询结果中迭代文档,更新单个字段并保存文档。
我的理解是我需要用光标迭代查询结果,获取记录的_id,然后在调用update()中传递的搜索查询中使用它;
下面的代码运行但值永远不会成功回到数据库。我将非常感谢任何帮助。在此先感谢。
我正在运行一个默认的开箱即用的Mongo实例。
以下是不起作用的代码块:
byte [] outputFile = new byte [10];
outputFile [0] = 40;
BasicDBObject newDocument = new BasicDBObject();
newDocument.append($ set,new BasicDBObject()。append(SomeField,outputFile));
String id = cursor.curr ()。get(_ id)。toString();
BasicDBObject searchQuery = new BasicDBObject()。append(_ id,id);
fileCollection.update(searchQuery,newDocument,true,false);
Hi,
I'm new to Mongo. I need to iterate documents in a query result, update a single field and save the document.
My understanding is that I need to iterate the query result with a cursor, get the record's _id, and then use that in the search query that is passed in the call to update();
The code below runs but the values never make it back to the database. I would be extremely grateful for any assistance. Thanks in advance.
I'm running a default out-of-the-box Mongo instance.
Here is the chunk of code that isn't working:
byte[] outputFile = new byte[10];
outputFile[0] = 40;
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("SomeField", outputFile));
String id = cursor.curr().get("_id").toString();
BasicDBObject searchQuery = new BasicDBObject().append("_id", id);
fileCollection.update(searchQuery, newDocument, true, false);
推荐答案
set,new BasicDBObject() .appen d(SomeField,outputFile));
String id = cursor.curr()。get(_ id)。toString();
BasicDBObject searchQuery = new BasicDBObject()。append(_ id,id);
fileCollection.update(searchQuery ,newDocument,true,false);
set", new BasicDBObject().append("SomeField", outputFile));
String id = cursor.curr().get("_id").toString();
BasicDBObject searchQuery = new BasicDBObject().append("_id", id);
fileCollection.update(searchQuery, newDocument, true, false);
这篇关于如何迭代MongoD集合并一次更新1条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!