本文整理汇总了Java中com.mongodb.DBCollection.findAndModify方法的典型用法代码示例。如果您正苦于以下问题:Java DBCollection.findAndModify方法的具体用法?Java DBCollection.findAndModify怎么用?Java DBCollection.findAndModify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.DBCollection
的用法示例。
在下文中一共展示了DBCollection.findAndModify方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNextId
import com.mongodb.DBCollection; //导入方法依赖的package包/类
private String getNextId(String collectionName) {
DBCollection seq = mongoTemplate.getCollection("seq");
DBObject query = new BasicDBObject();
query.put("_id", collectionName);
DBObject change = new BasicDBObject("seq", Integer.valueOf(1));
DBObject update = new BasicDBObject("$inc", change);
DBObject res = seq.findAndModify(query, new BasicDBObject(), new BasicDBObject(), false, update, true, true);
return res.get("seq").toString();
}
示例2: updateUser
import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
* Update an existing user.
*
* @param id The ID of the user to update.
* @param payload The fields of the user that should be updated.
* @return Nothing.
*/
@PUT
@Path("/{id}")
@Consumes("application/json")
public Response updateUser(@PathParam("id") String id, JsonObject payload) {
// Validate the JWT. The JWT should be in the 'users' group. We do not
// check to see if the user is modifying their own profile.
try {
validateJWT(new HashSet<String>(Arrays.asList("users")));
} catch (JWTException jwte) {
return Response.status(Status.UNAUTHORIZED)
.type(MediaType.TEXT_PLAIN)
.entity(jwte.getMessage())
.build();
}
// Retrieve the user from the database.
DB database = mongo.getMongoDB();
DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
DBObject oldDbUser = dbCollection.findOne(new ObjectId(id));
if (oldDbUser == null) {
return Response.status(Status.BAD_REQUEST).entity("The user was not Found.").build();
}
// If the input object contains a new password, need to hash it for use in the database.
User newUser = null;
if (payload.containsKey("password")) {
try {
String rawPassword = payload.getString("password");
String saltString = (String) (oldDbUser.get(User.JSON_KEY_USER_PASSWORD_SALT));
PasswordUtility pwUtil = new PasswordUtility(rawPassword, saltString);
JsonObject newJson =
createJsonBuilder(payload)
.add(User.JSON_KEY_USER_PASSWORD_HASH, pwUtil.getHashedPassword())
.add(User.JSON_KEY_USER_PASSWORD_SALT, pwUtil.getSalt())
.build();
newUser = new User(newJson);
} catch (Throwable t) {
return Response.serverError().entity("Error updating password").build();
}
} else {
newUser = new User(payload);
}
// Create the updated user object. Only apply the fields that we want the
// client to change (skip the internal fields).
DBObject updateObject = new BasicDBObject("$set", newUser.getDBObjectForModify());
dbCollection.findAndModify(oldDbUser, updateObject);
return Response.ok().build();
}
示例3: nextSequenceNumber
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public static Long nextSequenceNumber(String seqName) {
if (seqName == null)
throw new NullPointerException("A name must be provided when generating a sequence number.");
Object connObj = App.get().inject(Connections.class).getDefaultMerchantConnection();
if (connObj instanceof DB) {
DB db = (DB) connObj;
DBCollection col = db.getCollection("_sequence");
DBObject query = new BasicDBObject();
query.put("_id", seqName);
DBObject change = new BasicDBObject("seq", 1);
DBObject update = new BasicDBObject("$inc", change);
DBObject res = col.findAndModify(query, new BasicDBObject(), new BasicDBObject(), false, update, true,
true);
Object seq = res.get("seq");
if (seq != null) {
if (seq instanceof Number) {
return ((Number) seq).longValue();
} else if (seq instanceof String) {
return Long.parseLong((String) seq);
}
throw new IllegalStateException(
"Expecting a sequence of type Number but got " + seq.getClass().getName() + " instead");
} else {
throw new IllegalStateException("Expecting a sequence number but got NULL instead");
}
} else {
// TODO
}
return null;
}