當前位置: 首頁>>代碼示例>>Java>>正文


Java DBCollection.findAndModify方法代碼示例

本文整理匯總了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();
}
 
開發者ID:Saisimon,項目名稱:tip,代碼行數:10,代碼來源:MongoAppender.java

示例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();
}
 
開發者ID:OpenLiberty,項目名稱:sample-acmegifts,代碼行數:60,代碼來源:UserResource.java

示例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;
}
 
開發者ID:geetools,項目名稱:geeCommerce-Java-Shop-Software-and-PIM,代碼行數:42,代碼來源:SequenceGenerator.java


注:本文中的com.mongodb.DBCollection.findAndModify方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。