本文整理汇总了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;
}
示例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);
}
}
示例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"));
}
示例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();
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}
示例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());
}
示例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;
}
示例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);
}
示例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;
}
示例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();
}