当前位置: 首页>>代码示例>>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;未经允许,请勿转载。