Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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中更新MongoDb数据库?_Java_Mongodb_Web Applications - Fatal编程技术网

如何在Java中更新MongoDb数据库?

如何在Java中更新MongoDb数据库?,java,mongodb,web-applications,Java,Mongodb,Web Applications,我是MongoDb的新手,有很多关于在2.x版本中更新集合的例子,但我找不到任何关于3.x版本的源代码 JAVA代码: MongoClient mongoClient = new MongoClient("localhost",27017); MongoDatabase database = mongoClient.getDatabase("dbTest"); MongoCollection<Document> collection = database

我是MongoDb的新手,有很多关于在2.x版本中更新集合的例子,但我找不到任何关于3.x版本的源代码

JAVA代码:

    MongoClient mongoClient = new MongoClient("localhost",27017);
    MongoDatabase database = mongoClient.getDatabase("dbTest");
    MongoCollection<Document> collection =    database.getCollection("colTest");
    Document updateQuery = new Document();
    updateQuery.append("$set",
    new Document().append("_id", "test"));
    Document searchQuery = new Document();
    searchQuery.append("likes", "125");
    collection.updateMulti(searchQuery, updateQuery); //.updateMulti gives an error.
预期产出:

{ 
   "_id" : "test",
   "status" : 1,
   "time" : null,
   "instagram" :{
         "description" : "database",
         "likes" : 125,
         "url" : "http://www.instagram.com/",
         "by", "users"
   },
   "batchid" : 15000234
}

对于Mongodb java驱动程序:

使用updateOne方法 要根据筛选器更新集合中的单个文档

         collection.updateOne(searchQuery, updateQuery );
使用updateMany方法, 要根据筛选器更新集合中的多个文档

         collection.updateMany(searchQuery, updateQuery );
例如

        MongoClient client = new MongoClient("localhost",27017);
        MongoDatabase db = client.getDatabase("TestDB");
        MongoCollection<Document> collection = db.getCollection("test");
        Document query = new Document();
        query.append("_id","test");
        Document setData = new Document();
        setData.append("status", 1).append("instagram.likes", 125);
        Document update = new Document();
        update.append("$set", setData);
        //To update single Document  
        collection.updateOne(query, update);
MongoClient客户端=新的MongoClient(“localhost”,27017);
MongoDatabase db=client.getDatabase(“TestDB”);
MongoCollection collection=db.getCollection(“测试”);
单据查询=新建单据();
追加(“_id”,“test”);
文档集数据=新文档();
setData.append(“status”,1)。append(“instagram.likes”,125);
文档更新=新文档();
update.append(“$set”,setData);
//更新单个文档的步骤
collection.updateOne(查询、更新);
        MongoClient client = new MongoClient("localhost",27017);
        MongoDatabase db = client.getDatabase("TestDB");
        MongoCollection<Document> collection = db.getCollection("test");
        Document query = new Document();
        query.append("_id","test");
        Document setData = new Document();
        setData.append("status", 1).append("instagram.likes", 125);
        Document update = new Document();
        update.append("$set", setData);
        //To update single Document  
        collection.updateOne(query, update);