本文整理汇总了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);
}