Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

In this page you can find the example usage for com.mongodb DBCollection findOne.

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

public User findByLastName(String lastName) throws SystemException {
    String[] searchFields = { "lastName" };

    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    int i = 0;//from   w w  w. java  2  s  .  c o  m

    DBObject criteria = new QueryBuilder().put(searchFields[i++]).is(lastName).get();

    DBObject dbObject = collection.findOne(criteria);

    return getDocument(dbObject);
}

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

public User findByFN_LN(String firstName, String lastName) throws SystemException {
    String[] searchFields = { "firstName", "lastName" };

    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    int i = 0;// w ww  . j  ava  2  s . c  om

    DBObject criteria = new QueryBuilder().put(searchFields[i++]).is(firstName).put(searchFields[i++])
            .is(lastName).get();

    DBObject dbObject = collection.findOne(criteria);

    return getDocument(dbObject);
}

From source file:org.nmdp.hmlfhirconvertermodels.domain.base.CascadingUpdate.java

License:Open Source License

private IMongoDataRepositoryModel upsert(IMongoDataRepositoryModel model, MongoOperations mongoOperations,
        String collectionName) throws Exception {
    WriteResult writeResult = mongoOperations.upsert(buildUpsertQuery(model), buildUpsertUpdate(model),
            collectionName);//from w w  w  . ja  v a 2  s.co  m
    DBCollection collection = mongoOperations.getCollection(collectionName);
    Object upsertedId;

    if (writeResult.isUpdateOfExisting()) {
        try {
            Field field = model.getClass().getDeclaredField("id");
            field.setAccessible(true);
            String id = field.get(model).toString();
            upsertedId = objectifyId(id);
        } catch (Exception ex) {
            LOG.error(ex);
            throw new Exception("Unable to evaluate 'id' property", ex);
        }
    } else {
        upsertedId = writeResult.getUpsertedId();
    }

    DBObject result = collection.findOne(upsertedId);
    return model.convertGenericResultToModel(result, model, getDocumentProperties(model));
}

From source file:org.opendaylight.controller.samples.onftappingapp.CaptureDev.java

License:Apache License

public static boolean adjustReferenceCount(final ReferenceCountEnum adjType, String switchEntryId)
        throws NotFoundException {

    DB database = TappingApp.getDatabase();
    DBCollection table = database.getCollection(DatabaseNames.getCaptureDevTableName());

    // Look for the CaptureDev object by object ID in the database
    BasicDBObject searchQuery = new BasicDBObject();
    ObjectId id = new ObjectId(switchEntryId);
    searchQuery.put("_id", id);
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    // create an increment query
    DBObject modifier = new BasicDBObject("refCount", (adjType == ReferenceCountEnum.DECREMENT) ? -1 : 1);
    DBObject incQuery = new BasicDBObject("$inc", modifier);

    // increment a counter value atomically
    WriteResult result = table.update(searchQuery, incQuery);
    return (result != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.MatchCriteria.java

License:Apache License

public static boolean adjustReferenceCount(final ReferenceCountEnum adjType, String matchCriteriaId)
        throws NotFoundException {

    DB database = TappingApp.getDatabase();
    DBCollection table = database.getCollection(DatabaseNames.getMatchCriteriaTableName());

    // Look for the Match Criteria object by object ID in the database
    BasicDBObject searchQuery = new BasicDBObject();
    ObjectId id = new ObjectId(matchCriteriaId);
    searchQuery.put("_id", id);
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    // create an increment query
    DBObject modifier = new BasicDBObject("refCount", (adjType == ReferenceCountEnum.DECREMENT) ? -1 : 1);
    DBObject incQuery = new BasicDBObject("$inc", modifier);

    // increment a counter value atomically
    WriteResult result = table.update(searchQuery, incQuery);
    return (result != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.NextHopSwitch.java

License:Apache License

public static boolean adjustReference(final ReferenceCountEnum adjType, String nhsId, SwitchEntry tapPolicy)
        throws NotFoundException {

    DB database = TappingApp.getDatabase();
    DBCollection table = database.getCollection(DatabaseNames.getNextHopSwitchTableName());

    // Look for the SwitchEntry object by object ID in the database
    BasicDBObject searchQuery = new BasicDBObject();
    ObjectId id = new ObjectId(nhsId);
    searchQuery.put("_id", id);
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    // create an increment query
    DBObject modifier = new BasicDBObject("refCount", (adjType == ReferenceCountEnum.DECREMENT) ? -1 : 1);
    DBObject incQuery = new BasicDBObject("$inc", modifier);

    // increment a counter value atomically
    WriteResult result = table.update(searchQuery, incQuery);
    return (result != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.PortChain.java

License:Apache License

public static Boolean adjustReferenceCount(final ReferenceCountEnum adjType, String switchEntryId)
        throws NotFoundException {

    DB database = TappingApp.getDatabase();
    DBCollection table = database.getCollection(DatabaseNames.getPortChainTableName());

    // Look for the SwitchEntry object by object ID in the database
    BasicDBObject searchQuery = new BasicDBObject();
    ObjectId id = new ObjectId(switchEntryId);
    searchQuery.put("_id", id);
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    // create an increment query
    DBObject modifier = new BasicDBObject("refCount", (adjType == ReferenceCountEnum.DECREMENT) ? -1 : 1);
    DBObject incQuery = new BasicDBObject("$inc", modifier);

    // increment a counter value atomically
    WriteResult result = table.update(searchQuery, incQuery);
    return (result != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.SwitchEntry.java

License:Apache License

public static boolean adjustReferenceCount(final ReferenceCountEnum adjType, String switchEntryId)
        throws NotFoundException {

    DB database = TappingApp.getDatabase();
    DBCollection table = database.getCollection(DatabaseNames.getSwitchEntryTableName());

    // Look for the SwitchEntry object by object ID in the database
    BasicDBObject searchQuery = new BasicDBObject();
    ObjectId id = new ObjectId(switchEntryId);
    searchQuery.put("_id", id);
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    // create an increment query
    DBObject modifier = new BasicDBObject("refCount", (adjType == ReferenceCountEnum.DECREMENT) ? -1 : 1);
    DBObject incQuery = new BasicDBObject("$inc", modifier);

    // increment a counter value atomically
    WriteResult result = table.update(searchQuery, incQuery);
    return (result != null) ? true : false;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void deleteTapPolicy(String objectId) throws NotFoundException {
    DBCollection table = database.getCollection(DatabaseNames.getTapPolicyTableName());

    // Create a query to look for the existing TapPolicy by ID
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(objectId));
    DBObject dbObj = table.findOne(searchQuery);

    if (dbObj == null)
        throw new NotFoundException();

    // We are deleting this tapPolicy, so remove references to other objects
    TapPolicy.removeObjectReferences(dbObj);

    // Remove the TapPolicy from the database
    table.remove(searchQuery);//from  www. j  a  va  2s  . co m

    logger.info("Removed tap policy " + (String) dbObj.get("name"));

    // Notify the application that the tap policy changed
    // TODO - add objectID
    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.TAP_POLICY);
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

private boolean tapPolicyExistsByName(String name) {
    DBCollection table = database.getCollection(DatabaseNames.getTapPolicyTableName());

    // Run a query to look for the TapPolicy name
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("name", name);
    DBObject dbObj = table.findOne(searchQuery);

    return (dbObj != null) ? true : false;
}