本文整理匯總了Java中com.mongodb.client.MongoCollection.replaceOne方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoCollection.replaceOne方法的具體用法?Java MongoCollection.replaceOne怎麽用?Java MongoCollection.replaceOne使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.client.MongoCollection
的用法示例。
在下文中一共展示了MongoCollection.replaceOne方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: update
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
/**
* @decription 更新數據
* @author yi.zhang
* @time 2017年6月2日 下午6:19:08
* @param table 文檔名稱(表名)
* @param obj
* @return
*/
public int update(String table, Object obj) {
try {
if(session==null){
init(servers, database, schema, username, password);
}
MongoCollection<Document> collection = session.getCollection(table);
if (collection == null) {
return 0;
}
JSONObject json = JSON.parseObject(JSON.toJSONString(obj));
Document value = Document.parse(JSON.toJSONString(obj));
collection.replaceOne(Filters.eq("_id", json.containsKey("_id")?json.get("_id"):json.get("id")), value);
return 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
示例2: updateById
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public static Document updateById(MongoCollection<Document> col, Object id, Document newDoc,
boolean repalce) {
Bson filter = Filters.eq("_id", id);
if (repalce)
col.replaceOne(filter, newDoc);
else
col.updateOne(filter, new Document("$set", newDoc));
return newDoc;
}
示例3: saveRegionData
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public void saveRegionData(LoadedRegion loadedRegion) {
MongoCollection<Document> collection = getDatabase().getCollection("chunks");
Document regionDocument = new Document("_id", loadedRegion.getUniqueId().toString())
.append("owner", loadedRegion.getOwner().toString())
.append("world", loadedRegion.getWorld().getUniqueId().toString())
.append("type", loadedRegion.getType().toString())
.append("fromX", loadedRegion.getRegion().getFrom().getX())
.append("fromZ", loadedRegion.getRegion().getFrom().getZ())
.append("toX", loadedRegion.getRegion().getTo().getX())
.append("toZ", loadedRegion.getRegion().getTo().getZ())
.append("created", loadedRegion.getEpoch());
collection.replaceOne(
Filters.eq("_id", loadedRegion.getUniqueId().toString()),
regionDocument,
(new UpdateOptions()).upsert(true)
);
}
示例4: saveUserData
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public void saveUserData(UserData userData) {
MongoCollection<Document> collection = getDatabase().getCollection("users");
Document userDocument = new Document("_id", userData.getUniqueId())
.append("seen", userData.getLastSeen())
.append("joined", userData.getUserJoined());
collection.replaceOne(
Filters.eq("_id", userData.getUniqueId().toString()),
userDocument,
(new UpdateOptions()).upsert(true)
);
}
示例5: saveFailureStatusProcesssed
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public static void saveFailureStatusProcesssed(MongoDatabase mongoDatabase, long id, boolean processed) {
MongoCollection<Document> failureCollection = mongoDatabase.getCollection("failures");
failureCollection.replaceOne(
Filters.eq("tweet_id", id),
new Document().append("tweet_id", id).append("processed", processed),
new UpdateOptions().upsert(true));
}
示例6: replaceEntry
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
/**
* Replace a current db entry with another
* @param type
* @param old entry
* @param newPost entry
*/
public static void replaceEntry(Class type, DBEntry old, DBEntry newPost) {
MongoCollection collection = getCollectionForClass(type);
collection.replaceOne(eq("_id", old.getId()), newPost);
}