Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Mongo更新后获取受影响的行_Java_Mongodb - Fatal编程技术网

Java 在Mongo更新后获取受影响的行

Java 在Mongo更新后获取受影响的行,java,mongodb,Java,Mongodb,如何指示Mongo在执行更新后返回受影响的行? 现在它回来了 { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 12 , "updatedExisting" : true} 这意味着12条记录被更新了,但我想知道它们是哪些记录 一种解决方法是执行两个查询,第一个是查找_id,第二个是实际更新它们,但这还不够好 非常感谢 R.使用API。例如,根据这篇博文的指导原则,您可以将更新查询编写为: // findAndModify op

如何指示Mongo在执行更新后返回受影响的行? 现在它回来了

{ "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 12 , "updatedExisting" : true}
这意味着12条记录被更新了,但我想知道它们是哪些记录

一种解决方法是执行两个查询,第一个是查找_id,第二个是实际更新它们,但这还不够好

非常感谢

R.

使用API。例如,根据这篇博文的指导原则,您可以将更新查询编写为:

    // findAndModify operation. Update colour to blue for cars having speed < 45
    DBObject query = new BasicDBObject("speed",
            new BasicDBObject("$lt", 45));
    DBObject update = new BasicDBObject();
    update.put("$set", new BasicDBObject("color", "Blue"));
    DBCursor cursor = coll.find();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }
    coll.findAndModify(query, update); 
//查找和修改操作。将车速小于45的车辆颜色更新为蓝色
DBObject query=newBasicDBObject(“速度”,
新的基本目标($lt),45);
DBObject update=new BasicDBObject();
update.put(“$set”,新的基本对象(“颜色”,“蓝色”);
DBCursor cursor=coll.find();
试一试{
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}最后{
cursor.close();
}
coll.findAndModify(查询、更新);

我可以对find()使用相同的查询。但这需要运行两个查询。问题是我可以在同一个原子查询中查找和更新,它只返回更新的行。使用API。这里有一个很好的示例:。谢谢@chridam,这就是我要找的。如果你把你的评论变成回答,我会接受的。