本文整理汇总了Java中com.mongodb.DBCollection.update方法的典型用法代码示例。如果您正苦于以下问题:Java DBCollection.update方法的具体用法?Java DBCollection.update怎么用?Java DBCollection.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.DBCollection
的用法示例。
在下文中一共展示了DBCollection.update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNextId
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public int getNextId(GridFS destDatabase) {
DBCollection countersCollection = destDatabase.getDB().getCollection("counters");
DBObject record = countersCollection.findOne(new BasicDBObject("_id", "package"));
if (record == null) {
BasicDBObject dbObject = new BasicDBObject("_id", "package");
dbObject.append("seq", 0);
countersCollection.insert(dbObject);
record = dbObject;
}
int oldID = (int) record.get("seq");
int newID = oldID + 1;
record.put("seq", newID);
countersCollection.update(new BasicDBObject("_id", "package"), record);
return newID;
}
示例2: updateUserChangeNameAndSurnametoName
import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
* update 6: {@link UserDocument} has changed; 'name' and 'surname' will be concat to 'name'.
* for each every user document get 'name' and 'surname', concat them, update 'name', remove field surname and update document.
*
* @since V7
*/
@ChangeSet(order = "008", id = "updateUserChangeNameAndSurnameToName", author = "admin")
public void updateUserChangeNameAndSurnametoName(final MongoTemplate template) {
final DBCollection userCollection = template.getCollection("user");
final Iterator<DBObject> cursor = userCollection.find();
while (cursor.hasNext()) {
final DBObject current = cursor.next();
final Object nameObj = current.get("name");
final Object surnameObj = current.get("surname");
final String updateName = (nameObj != null ? nameObj.toString() : "") + " " + (surnameObj != null ? surnameObj.toString() : "");
final BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$set", new BasicDBObject("name", updateName));
updateQuery.append("$unset", new BasicDBObject("surname", ""));
final BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("_id", current.get("_id"));
userCollection.update(searchQuery, updateQuery);
}
}
示例3: addRate
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void addRate(Rating rating) {
DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(getId()));
ratings.add(rating);
// Update module's average
double average = 0;
for (Rating r : ratings) {
average += r.getRate();
}
average = average / ratings.size();
DBObject toSet = new BasicDBObject();
toSet.put(FIELD_RATINGS, ratings.toDBObject());
toSet.put(FIELD_AVERAGE_RATE, ((double) average));
collection.update(query, new BasicDBObject("$set", toSet), true, false);
}
示例4: save
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void save() throws SinfonierException {
DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
if (this.id == null) {
Logger.info("Adding a new module version");
DBObject dbObj = this.toDBObject();
collection.save(dbObj);
this.id = dbObj.get(FIELD_ID).toString();
} else {
Logger.info("Editing module version id:" + this.getId().toString());
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(this.id));
DBObject toSet = this.toDBObject();
// Remove fields's unmodified
toSet.removeField(FIELD_ID);
toSet.removeField(FIELD_CREATED);
toSet.removeField(FIELD_TOPOLOGIES_COUNT);
toSet.removeField(FIELD_BUILD_STATUS);
// Update field's update
toSet.put(FIELD_UPDATED, new Date());
collection.update(query, new BasicDBObject("$set", toSet), true, false);
}
}
示例5: doPut
import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
* Perform a single 'put' operation on the local object store.
*
* @param ref Object reference string of the object to be written.
* @param obj JSON string encoding the object to be written.
* @param collection Collection to put to.
*
* @return a ResultDesc object describing the success or failure of the
* operation.
*/
private ResultDesc doPut(String ref, String obj, DBCollection collection,
boolean requireNew)
{
String failure = null;
if (obj == null) {
failure = "no object data given";
} else {
try {
DBObject objectToWrite = jsonLiteralToDBObject(obj, ref);
if (requireNew) {
WriteResult wr = collection.insert(objectToWrite);
} else {
DBObject query = new BasicDBObject();
query.put("ref", ref);
collection.update(query, objectToWrite, true, false);
}
} catch (Exception e) {
failure = e.getMessage();
}
}
return new ResultDesc(ref, failure);
}
示例6: doUpdate
import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
* Perform a single 'update' operation on the local object store.
*
* @param ref Object reference string of the object to be written.
* @param version Expected version number of object before updating.
* @param obj JSON string encoding the object to be written.
* @param collection Collection to put to.
*
* @return an UpdateResultDesc object describing the success or failure of
* the operation.
*/
private UpdateResultDesc doUpdate(String ref, int version, String obj,
DBCollection collection)
{
String failure = null;
boolean atomicFailure = false;
if (obj == null) {
failure = "no object data given";
} else {
try {
DBObject objectToWrite = jsonLiteralToDBObject(obj, ref);
DBObject query = new BasicDBObject();
query.put("ref", ref);
query.put("version", version);
WriteResult result =
collection.update(query, objectToWrite, false, false);
if (result.getN() != 1) {
failure = "stale version number on update";
atomicFailure = true;
}
} catch (Exception e) {
failure = e.getMessage();
}
}
return new UpdateResultDesc(ref, failure, atomicFailure);
}
示例7: save
import com.mongodb.DBCollection; //导入方法依赖的package包/类
private void save(MongoDBCacheDocument doc, long now) {
DBCollection coll = getCollection();
if(now<=0)now = System.currentTimeMillis();
doc.setLastAccessed(now);
doc.setLastUpdated(now);
doc.addHit();
/*
* very atomic updated. Just the changed values are sent to db.
* If the doc do not exists is inserted.
*/
BasicDBObject q = new BasicDBObject("key", doc.getKey());
coll.update(q, doc.getDbObject(), true, false);
}
示例8: addComplain
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void addComplain(Inappropriate inappropriate) {
DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(getId()));
complains.add(inappropriate);
DBObject toSet = new BasicDBObject();
toSet.put(FIELD_COMPLAINS, complains.toDBObject());
collection.update(query, new BasicDBObject("$set", toSet), true, false);
}
示例9: addToMyTools
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void addToMyTools(MyTool tool) {
DBObject toSet = new BasicDBObject();
DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(getId()));
myTools.add(tool);
toSet.put(FIELD_MY_TOOLS, myTools.toDBObject());
collection.update(query, new BasicDBObject("$set", toSet), true, false);
}
示例10: removeToMyTools
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void removeToMyTools(MyTool tool) {
DBObject toSet = new BasicDBObject();
DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(getId()));
myTools.remove(tool);
toSet.put(FIELD_MY_TOOLS, myTools.toDBObject());
collection.update(query, new BasicDBObject("$set", toSet), true, false);
}
示例11: save
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public String save() throws SinfonierException {
try {
DBCollection collection = MongoFactory.getDB().getCollection(getCollectionName());
if (id == null) {
Logger.info("New topology");
DBObject dbObj = this.toDBObject();
updateUsedModulesCount(true);
collection.save(dbObj);
this.id = dbObj.get(FIELD_ID).toString();
} else {
Logger.info("Editing topology id:" + this.getId());
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(this.getId()));
DBObject toSet = this.toDBObject();
// Remove fields's unmodified
toSet.removeField(FIELD_ID);
toSet.removeField(FIELD_CREATED);
toSet.removeField(FIELD_AUTHOR_ID);
// Update field's update
toSet.put(FIELD_UPDATED, new Date());
updateUsedModulesCount(false);
collection.update(query, new BasicDBObject("$set", toSet), true, false);
}
return this.getId();
} catch (MongoException.DuplicateKey e) {
Logger.error(e, e.getMessage());
throw new SinfonierException(SinfonierError.TOPOLOGY_DUPLICATE, e, this.name);
}
}
示例12: removeFromArrayFiled
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public static void removeFromArrayFiled(DB db, String collectionName, DBObject query, String field, Object value) {
DBCollection col = db.getCollection(collectionName);
DBObject fieldClause = new BasicDBObject();
fieldClause.put(field, value);
DBObject pullClause = new BasicDBObject();
pullClause.put("$pull", fieldClause);
col.update(query, pullClause, false, true);
}
示例13: updateReadCounter
import com.mongodb.DBCollection; //导入方法依赖的package包/类
private void updateReadCounter(DBCollection col, Object _id) {
try {
DBObject readCountUpdate = new BasicDBObject("$inc", new BasicDBObject(GlobalColumn.READ_COUNT, 1));
readCountUpdate.put("$set", new BasicDBObject(GlobalColumn.READ_COUNT_DATE, DateTimes.newDate()));
col.update(new BasicDBObject(GlobalColumn.ID, _id), readCountUpdate);
} catch (Throwable t) {
throw new DaoException(t);
}
}
示例14: run
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@Override
public void run() {
MongoCollection collection = getMongoCollection();
DBCollection dbCollection = collection.getDBCollection();
String field = getField();
DBObject object = getObject();
BasicDBObject append = new BasicDBObject();
append.append("$set", object);
Object id = object.get(field);
if(Settings.getInstance().isDebug()) {
System.out.println("(" + dbCollection.getName() + "): Upserting document: (field:" + field + " id:" + id + ")");
}
dbCollection.update(new BasicDBObject(field, object.get(field)), append, true, false);
}
示例15: updateTopologiesCount
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void updateTopologiesCount() {
DBObject toSet = new BasicDBObject(FIELD_TOPOLOGIES_COUNT, getTopologiesCount());
DBObject query = new BasicDBObject(FIELD_ID, new ObjectId(getId()));
DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
collection.update(query, new BasicDBObject("$set", toSet));
}