Example usage for com.mongodb.client.model Filters eq

List of usage examples for com.mongodb.client.model Filters eq

Introduction

In this page you can find the example usage for com.mongodb.client.model Filters eq.

Prototype

public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates a filter that matches all documents where the value of the field name equals the specified value.

Usage

From source file:org.seadpdt.impl.RepoServicesImpl.java

License:Apache License

@GET
@Path("/{id}/researchobjects")
@Produces(MediaType.APPLICATION_JSON)// ww  w . j a  v a2  s .  co  m
public Response getROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(
                Filters.and(Filters.eq("Repository", id), Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection
                .find(Filters.and(Filters.eq("Repository", id), Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(Filters.eq("Repository", id));
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        array.add(cursor.next());
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.impl.RepoServicesImpl.java

License:Apache License

@GET
@Path("/{id}/researchobjects/new")
@Produces(MediaType.APPLICATION_JSON)// w w w .  ja v a  2s .c  o  m
public Response getNewROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);

    //Match ROs with no status from any repo
    Document match = new Document("Repository", id);
    Document reporter = new Document("reporter", id);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    match.put("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        Document nextDoc = cursor.next();
        array.add(nextDoc);
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)/*from  www .j  a  va2s  . co  m*/
public Response getROsList(@QueryParam("Purpose") final String purpose) {
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(Filters.ne("Preferences.Purpose", "Testing-Only"));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.eq("Preferences.Purpose", purpose));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find();
    }
    iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Title", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        array.put(JSON.parse(cursor.next().toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/new/")
@Produces(MediaType.APPLICATION_JSON)//from   w w w.j  av  a2  s  .  co m
public Response getNewROsList(@QueryParam("Purpose") final String purpose) {
    //Find ROs that have a status not from the services and don't include them :-)
    Document reporterRule = new Document("$ne", Constants.serviceName);
    Document reporter = new Document("reporter", reporterRule);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    Document match = new Document("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }
    iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Title", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        array.put(JSON.parse(cursor.next().toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.seadpdt.RepoServices.java

License:Apache License

@GET
@Path("/{id}/researchobjects")
@Produces(MediaType.APPLICATION_JSON)/*from  w  w w. j a  va 2 s .  c  o m*/
public Response getROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(
                Filters.and(Filters.eq("Repository", id), Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection
                .find(Filters.and(Filters.eq("Repository", id), Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(Filters.eq("Repository", id));
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        array.add(cursor.next());
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.RepoServices.java

License:Apache License

@GET
@Path("/{id}/researchobjects/new")
@Produces(MediaType.APPLICATION_JSON)//from   w  w w.  j  av  a  2  s .co  m
public Response getNewROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);

    //Match ROs with no status from any repo
    Document match = new Document("Repository", id);
    Document reporter = new Document("reporter", id);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    match.put("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        Document nextDoc = cursor.next();
        array.add(nextDoc);
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.springframework.data.mongodb.core.ReactiveMongoTemplate.java

License:Apache License

protected Mono<Object> saveDocument(final String collectionName, final Document document,
        final Class<?> entityClass) {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Saving Document containing fields: " + document.keySet());
    }//  w w w  .j a va 2s  .  c o  m

    return createMono(collectionName, collection -> {

        MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName,
                entityClass, document, null);
        WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);

        Publisher<?> publisher;
        if (!document.containsKey(ID_FIELD)) {
            if (writeConcernToUse == null) {
                publisher = collection.insertOne(document);
            } else {
                publisher = collection.withWriteConcern(writeConcernToUse).insertOne(document);
            }
        } else if (writeConcernToUse == null) {
            publisher = collection.replaceOne(Filters.eq(ID_FIELD, document.get(ID_FIELD)), document,
                    new UpdateOptions().upsert(true));
        } else {
            publisher = collection.withWriteConcern(writeConcernToUse).replaceOne(
                    Filters.eq(ID_FIELD, document.get(ID_FIELD)), document, new UpdateOptions().upsert(true));
        }

        return Mono.from(publisher).map(o -> document.get(ID_FIELD));
    });
}

From source file:org.trade.core.persistence.local.mongo.MongoPersistence.java

License:Apache License

@Override
public byte[] loadBinaryData(String collectionName, String identifier) throws Exception {
    byte[] data = new byte[0];

    MongoClient client = new MongoClient(new MongoClientURI(this.mongoUrl));
    MongoDatabase db = client.getDatabase(this.dbName);

    Document doc = db.getCollection(collectionName).find(Filters.eq(IDENTIFIER_FIELD, identifier)).limit(1)
            .first();/*from w ww.j ava 2  s  . co m*/

    if (doc != null) {
        if (doc.containsKey(DATA_FIELD)) {
            data = ((Binary) doc.get(DATA_FIELD)).getData();
        } else {
            logger.info("Model object '{}' from model collection '{}' does not have any associated data at the "
                    + "moment.", identifier, collectionName);
        }
    } else {
        logger.info("The database does not know the specified model object '{}' from model collection '{}'",
                identifier, collectionName);
    }

    client.close();

    return data;
}

From source file:org.trade.core.persistence.local.mongo.MongoPersistence.java

License:Apache License

@Override
public void storeBinaryData(byte[] data, String collectionName, String identifier) throws Exception {
    MongoClient client = new MongoClient(new MongoClientURI(this.mongoUrl));
    MongoDatabase db = client.getDatabase(this.dbName);

    MongoCollection<Document> collection = db.getCollection(collectionName);
    Document doc = collection.find(Filters.eq(IDENTIFIER_FIELD, identifier)).limit(1).first();

    if (data == null) {
        // We assume that if the value is set to null, we should delete also the corresponding database entry
        if (doc != null) {
            collection.deleteOne(Filters.eq(IDENTIFIER_FIELD, identifier));
        }//from   w  ww  .ja  va  2  s.c  om
    } else {
        // Check if the document already exists and update it
        if (doc != null) {
            collection.updateOne(Filters.eq(IDENTIFIER_FIELD, identifier),
                    Updates.combine(Updates.set(DATA_FIELD, data), Updates.currentDate("lastModified")));
        } else {
            Document document = new Document(IDENTIFIER_FIELD, identifier).append(DATA_FIELD, data)
                    .append("lastModified", new Date());
            collection.insertOne(document);
        }
    }

    client.close();
}

From source file:org.trade.core.persistence.local.mongo.MongoPersistence.java

License:Apache License

@Override
public void deleteBinaryData(String collectionName, String identifier) throws Exception {
    MongoClient client = new MongoClient(new MongoClientURI(this.mongoUrl));
    MongoDatabase db = client.getDatabase(this.dbName);

    Document doc = db.getCollection(collectionName).findOneAndDelete(Filters.eq(IDENTIFIER_FIELD, identifier));

    if (doc != null) {
        logger.info("Model object '{}' from model collection '{}' and its associated data successfully deleted "
                + "from DB.", identifier, collectionName);
    } else {/*from w w  w  .j  av  a2s  . c om*/
        logger.info("The database does not know the specified model object '{}' from model collection '{}'",
                identifier, collectionName);
    }

    client.close();
}