Example usage for com.mongodb QueryBuilder QueryBuilder

List of usage examples for com.mongodb QueryBuilder QueryBuilder

Introduction

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

Prototype

public QueryBuilder() 

Source Link

Document

Creates a builder with an empty query

Usage

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

License:Open Source License

public Car findByVIN(String vIN) throws SystemException {
    String[] searchFields = { "VIN" };

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

    int i = 0;/*from  w  w w  .  j a  va2 s.  c  om*/

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

    DBObject dbObject = collection.findOne(criteria);

    return getDocument(dbObject);
}

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

License:Open Source License

public List<Car> findByMake(String make, int start, int end) throws SystemException {
    if (start < 0) {
        start = 0;// w  ww . jav  a 2s.c  o m
    }

    if ((end - start) < 0) {
        end = start + LIMIT;
    }

    String[] searchFields = { "make" };

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

    int i = 0;

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

    DBCursor dbCursor = collection.find(criteria).skip(start).limit(end - start);

    List<Car> list = new ArrayList<Car>();

    for (DBObject dbObject : dbCursor) {
        list.add(getDocument(dbObject));
    }

    return list;
}

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

License:Open Source License

protected void removeImpl(String id) throws UpdateException, SystemException {
    DBObject criteria = new QueryBuilder().put("_id").is(id).get();

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

    WriteResult writeResult = collection.remove(criteria);

    String err = writeResult.getError();

    if (err != null) {
        throw new UpdateException(err);
    }//from  ww w .ja v  a  2  s.  com
}

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

License:Open Source License

protected Car updateImpl(org.mongoj.samples.model.Car car) throws UpdateException, SystemException {
    DBCollection collection = getDB().getCollection(CarImpl.COLLECTION_NAME);

    if (car.isNew()) {
        car.setNew(false);//from  www .  j a  v a 2s . c om

        CarImpl carImpl = (CarImpl) car;

        carImpl.addMap.clear();
        carImpl.appendMap.clear();
        carImpl.removeMap.clear();
        carImpl.setMap.clear();

        WriteResult writeResult = collection.insert(getDBObject(car));

        String err = writeResult.getError();

        if (err != null) {
            throw new UpdateException(err);
        }
    } else {
        DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(car.getId())).get();

        CarImpl carImpl = (CarImpl) car;

        BasicDBObjectBuilder updateBuilder = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder setUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushAllUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder addUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeAllUpdates = BasicDBObjectBuilder.start();

        for (String field : carImpl.setMap.keySet()) {
            setUpdates = setUpdates.add(field, carImpl.setMap.get(field));
        }

        if (!setUpdates.isEmpty()) {
            updateBuilder.add(SET_OPERATOR, setUpdates.get());
        }

        for (String field : carImpl.appendMap.keySet()) {
            List<Object> list = (List<Object>) carImpl.appendMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    pushUpdates = pushUpdates.add(field, ((List) carImpl.appendMap.get(field)).get(0));
                } else {
                    pushAllUpdates = pushAllUpdates.add(field, carImpl.appendMap.get(field));
                }
            }
        }

        if (!pushUpdates.isEmpty()) {
            updateBuilder.add(PUSH_OPERATOR, pushUpdates.get());
        }

        if (!pushAllUpdates.isEmpty()) {
            updateBuilder.add(PUSH_ALL_OPERATOR, pushAllUpdates.get());
        }

        for (String field : carImpl.addMap.keySet()) {
            List<Object> list = (List<Object>) carImpl.addMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    addUpdates = addUpdates.add(field, ((List) carImpl.addMap.get(field)).get(0));
                } else {
                    DBObject each = BasicDBObjectBuilder.start()
                            .add(EACH_OPERATOR, ((List) carImpl.addMap.get(field)).toArray()).get();

                    addUpdates = addUpdates.add(field, each);
                }
            }
        }

        if (!addUpdates.isEmpty()) {
            updateBuilder.add(ADD_TO_SET_OPERATOR, addUpdates.get());
        }

        for (String field : carImpl.removeMap.keySet()) {
            List<Object> list = (List<Object>) carImpl.removeMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    removeUpdates = removeUpdates.add(field, ((List) carImpl.removeMap.get(field)).get(0));
                } else {
                    removeAllUpdates = removeAllUpdates.add(field, carImpl.removeMap.get(field));
                }
            }
        }

        if (!removeUpdates.isEmpty()) {
            updateBuilder.add(PULL_OPERATOR, removeUpdates.get());
        }

        if (!removeAllUpdates.isEmpty()) {
            updateBuilder.add(PULL_ALL_OPERATOR, removeAllUpdates.get());
        }

        if (!updateBuilder.isEmpty()) {
            DBObject update = updateBuilder.get();

            _log.debug("Update query = {}", update);

            WriteResult writeResult = collection.update(criteria, update);

            String err = writeResult.getError();

            if (err != null) {
                throw new UpdateException(err);
            }
        }
    }

    return car;
}

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

License:Open Source License

public User fetchByPrimaryKey(Serializable primaryKey) throws SystemException {
    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(primaryKey.toString())).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 findByUserId(long userId) throws SystemException {
    String[] searchFields = { "userId" };

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

    int i = 0;//from w w  w  .j  ava2  s  .co m

    DBObject criteria = new QueryBuilder().put(searchFields[i++]).is(userId).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 findByLastName(String lastName) throws SystemException {
    String[] searchFields = { "lastName" };

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

    int i = 0;/*from   ww  w . j  ava  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  w  w.ja v a 2  s. co m

    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.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

public List<User> findByFN_DOB(String firstName, Date dob, int start, int end) throws SystemException {
    if (start < 0) {
        start = 0;/*ww w  .j a va 2 s . c  om*/
    }

    if ((end - start) < 0) {
        end = start + LIMIT;
    }

    String[] searchFields = { "firstName", "info.dob" };

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

    int i = 0;

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

    DBCursor dbCursor = collection.find(criteria).skip(start).limit(end - start);

    List<User> list = new ArrayList<User>();

    for (DBObject dbObject : dbCursor) {
        list.add(getDocument(dbObject));
    }

    return list;
}

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

License:Open Source License

protected void removeImpl(String id) throws UpdateException, SystemException {
    DBObject criteria = new QueryBuilder().put("_id").is(id).get();

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

    WriteResult writeResult = collection.remove(criteria);

    String err = writeResult.getError();

    if (err != null) {
        throw new UpdateException(err);
    }//from  w  w  w. ja  v a2 s.c  o m
}