当前位置: 首页>>代码示例>>Java>>正文


Java DBCollection.findOne方法代码示例

本文整理汇总了Java中com.mongodb.DBCollection.findOne方法的典型用法代码示例。如果您正苦于以下问题:Java DBCollection.findOne方法的具体用法?Java DBCollection.findOne怎么用?Java DBCollection.findOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.mongodb.DBCollection的用法示例。


在下文中一共展示了DBCollection.findOne方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
 
开发者ID:roscisz,项目名称:KernelHive,代码行数:18,代码来源:DataManager.java

示例2: selectOne

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public DBObject selectOne(DBCollection collection) {
	DBObject fields = getFields();
	DBObject query = getQuery();
	DBObject orderByObject = getOrderByObject();

	// 日志
	log(fields, query, orderByObject);

	if (null == fields && null == orderByObject) {
		return collection.findOne(query);
	} else if (null != fields && null == orderByObject) {
		return collection.findOne(query, fields);
	} else {
		return collection.findOne(query, fields, orderByObject);
	}
}
 
开发者ID:xsonorg,项目名称:tangyuan2,代码行数:17,代码来源:SelectVo.java

示例3: testFindOne

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void testFindOne() throws UnknownHostException {
	MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print");
	DBCollection collection = client.getCollection("marc");
	BasicDBObject doc = createTestObject();
	collection.insert(doc);
	assertEquals(1, collection.count());
	DBObject myDoc = collection.findOne();
	assertEquals("MongoDB", myDoc.get("name"));
	assertEquals("database", myDoc.get("type"));
	assertEquals(1, myDoc.get("count"));
	assertEquals(BasicDBObject.class, myDoc.get("info").getClass());
	assertEquals(new BasicDBObject("x", 203).append("y", 102), myDoc.get("info"));
	assertEquals(203, ((BasicDBObject)myDoc.get("info")).get("x"));
	assertEquals(Integer.class, ((BasicDBObject)myDoc.get("info")).get("x").getClass());
	System.out.println(myDoc);
	collection.remove(new BasicDBObject("name", "MongoDB"));
}
 
开发者ID:pkiraly,项目名称:metadata-qa-marc,代码行数:18,代码来源:MarcMongodbClientTest.java

示例4: deleteUser

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
 * Delete a user.
 *
 * @param id The ID of the user to delete.
 * @return Nothing.
 */
@DELETE
@Path("/{id}")
public Response deleteUser(@PathParam("id") String id) {
  // Validate the JWT.  The JWT must be in the 'users' group.  We do not check
  // to see if the user is deleting 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);
  ObjectId dbId = new ObjectId(id);
  DBObject dbUser = dbCollection.findOne(dbId);

  // If the user did not exist, return an error.  Otherwise, remove the user.
  if (dbUser == null) {
    return Response.status(Status.BAD_REQUEST).entity("The user name was not Found.").build();
  }

  dbCollection.remove(new BasicDBObject(User.DB_ID, dbId));
  return Response.ok().build();
}
 
开发者ID:OpenLiberty,项目名称:sample-acmegifts,代码行数:35,代码来源:UserResource.java

示例5: getUser

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
 * Retrieve a user's profile.
 *
 * @param id The ID of the user.
 * @return The user's profile, as a JSON object. Private fields such as password and salt are not
 *     returned.
 */
@GET
@Path("/{id}")
@Produces("application/json")
public Response getUser(@PathParam("id") String id) {
  // Validate the JWT.  The JWT must belong to the 'users' or 'orchestrator' group.
  // We do not check if the user is retrieving their own profile, or someone else's.
  try {
    validateJWT(new HashSet<String>(Arrays.asList("users", "orchestrator")));
  } 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 user = dbCollection.findOne(new ObjectId(id));

  // If the user did not exist, return an error.  Otherwise, only return the public
  // fields (exclude things like the password).
  if (user == null) {
    return Response.status(Status.BAD_REQUEST).entity("The user not Found.").build();
  }

  JsonObject responsePayload = new User(user).getPublicJsonObject();

  return Response.ok(responsePayload, MediaType.APPLICATION_JSON).build();
}
 
开发者ID:OpenLiberty,项目名称:sample-acmegifts,代码行数:38,代码来源:UserResource.java

示例6: findOne

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private static Module findOne(DBObject query) throws SinfonierException {
  DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
  DBObject module = collection.findOne(query);

  if (module != null) {
    return new Module(module);
  }

  return null;
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:11,代码来源:Module.java

示例7: findOne

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private static ModuleVersion findOne(DBObject query) throws SinfonierException {
  DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
  DBObject module = collection.findOne(query);

  if (module != null) {
    return new ModuleVersion(module);
  }

  return null;
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:11,代码来源:ModuleVersion.java

示例8: findOne

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private static Topology findOne(DBObject query) throws SinfonierException {
  DBCollection collection = MongoFactory.getDB().getCollection(getCollectionName());
  DBObject topology = collection.findOne(query);

  if (topology != null) {
    return new Topology(topology);
  }
  return null;
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:10,代码来源:Topology.java

示例9: test01NewModuleWithDBObject

import com.mongodb.DBCollection; //导入方法依赖的package包/类
@Test
public void test01NewModuleWithDBObject() throws SinfonierException {
  // Prepare inputs
  DBCollection collection = MongoFactory.getDB().getCollection(TestData.MODULES_COLLECTION);
  DBObject query = new BasicDBObject(FIELD_ID, new ObjectId("57ad73e8e1c0801a5832424b"));
  DBObject dbObject = collection.findOne(query);
  
  // Run method
  Module result = new Module(dbObject);
  
  // Check result
  assertNotNull(result);
  assertEquals(3.5, result.getAverageRate(), TestData.OFFSET_DOUBLE);
  assertEquals("[email protected]", result.getAuthor().getId());
  assertEquals("[email protected]", result.getAuthorId());
  assertEquals("Bolts", result.getCategory());
  assertEquals(0, result.getComplains().size());
  assertEquals("2016-08-12 08:59:52", formatter.format(result.getCreatedAt()));
  assertNull(result.getIcon());
  assertEquals("java", result.getLanguage());
  assertEquals("MyBolt", result.getName());
  assertEquals(2, result.getRatings().size());
  assertEquals("published", result.getStatus());
  assertEquals(9, result.getTopologiesCount());
  assertEquals("bolt", result.getType());
  assertEquals("2016-08-12 09:01:51", formatter.format(result.getUpdatedAt()));
  assertEquals(1, result.getVersions().size());
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:29,代码来源:ModuleTest.java

示例10: test02NewModuleWithDBObject

import com.mongodb.DBCollection; //导入方法依赖的package包/类
@Test
public void test02NewModuleWithDBObject() throws SinfonierException {
  // Prepare inputs
  DBCollection collection = MongoFactory.getDB().getCollection(TestData.MODULE_VERSIONS_COLLECTION);
  DBObject query = new BasicDBObject(FIELD_ID, new ObjectId("57ad72bde1c0801a58324247"));
  DBObject dbObject = collection.findOne(query);
  
  // Run method
  ModuleVersion result = new ModuleVersion(dbObject);
  
  // Check result
  assertNotNull(result);
  assertEquals("success", result.getBuildStatus());
  assertEquals("WireIt.FormContainer", result.getContainer().getXType());
  assertEquals("2016-08-12 08:54:53", formatter.format(result.getCreatedAt()));
  assertEquals("Descripción del módulo MySpout. Versión 1.0", result.getDescription());
  assertEquals(0, result.getFields().size());
  assertEquals(0, result.getLibraries().size());
  assertEquals(0, result.getMyTools().size());
  assertTrue(result.getSourceCode().startsWith("/**The MIT License (MIT) "));
  assertEquals("", result.getSourceCodeURL());
  assertEquals("template", result.getSourceType());
  assertEquals("private", result.getStatus());
  assertNull(result.getTickTuple());
  assertEquals(1, result.getTopologiesCount());
  assertEquals("2016-08-12 08:56:43", formatter.format(result.getUpdatedAt()));
  assertEquals(1, result.getVersionCode());
  assertEquals("1.0", result.getVersionTag());
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:30,代码来源:ModuleVersionTest.java

示例11: findOne

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
   * 条件查询, 单条
   * @param dbName
   * @param query
   * @param fields
   * @return
   */
  public static DBObject findOne(String dbName, String collName, DBObject query, DBObject fields) {
  	DBCollection collection = getCollection(dbName, collName);
  	if (collection != null) {
	return collection.findOne(query, fields);
}
      return null;
  }
 
开发者ID:xuxueli,项目名称:xxl-incubator,代码行数:15,代码来源:MongoDBUtil.java

示例12: add

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void add(JsonObject meeting) {
	DBCollection coll = getColl();
	DBObject existing = coll.findOne(meeting.getString("id"));
	DBObject obj = MeetingsUtil.meetingAsMongo(meeting, existing);
	coll.save(obj);

}
 
开发者ID:WASdev,项目名称:sample.microprofile.meetingapp,代码行数:8,代码来源:MeetingManager.java

示例13: doGet

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
 * Perform a single 'get' operation on the local object store.
 *
 * @param ref  Object reference string of the object to be gotten.
 * @param collection  Collection to get from.
 *
 * @return a list of ObjectDesc objects, the first of which will be
 *    the result of getting 'ref' and the remainder, if any, will be the
 *    results of getting any contents objects.
 */
private List<ObjectDesc> doGet(String ref, DBCollection collection) {
    List<ObjectDesc> results = new LinkedList<ObjectDesc>();

    String failure = null;
    String obj = null;
    List<ObjectDesc> contents = null;
    try {
        DBObject query = new BasicDBObject();
        query.put("ref", ref);
        DBObject dbObj = collection.findOne(query);
        if (dbObj != null) {
            JSONObject jsonObj = dbObjectToJSONObject(dbObj);
            obj = jsonObj.sendableString();
            contents = doGetContents(jsonObj, collection);
        } else {
            failure = "not found";
        }
    } catch (Exception e) {
        obj = null;
        failure = e.getMessage();
    }

    results.add(new ObjectDesc(ref, obj, failure));
    if (contents != null) {
        results.addAll(contents);
    }
    return results;
}
 
开发者ID:FUDCo,项目名称:Elko,代码行数:39,代码来源:MongoObjectStore.java

示例14: 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


注:本文中的com.mongodb.DBCollection.findOne方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。