List of usage examples for com.mongodb.client MongoCollection find
FindIterable<TDocument> find(ClientSession clientSession);
From source file:com.sitewhere.user.persistence.mongodb.MongoUserManagement.java
License:Open Source License
@Override public List<IUser> listUsers(IUserSearchCriteria criteria) throws SiteWhereException { try {// w w w . j ava 2 s . c o m MongoCollection<Document> users = getMongoClient().getUsersCollection(); Document dbCriteria = new Document(); if (!criteria.isIncludeDeleted()) { MongoSiteWhereEntity.setDeleted(dbCriteria, false); } FindIterable<Document> found = users.find(dbCriteria) .sort(new BasicDBObject(MongoUser.PROP_USERNAME, 1)); MongoCursor<Document> cursor = found.iterator(); List<IUser> matches = new ArrayList<IUser>(); try { while (cursor.hasNext()) { Document match = cursor.next(); matches.add(MongoUser.fromDocument(match)); } } finally { cursor.close(); } return matches; } catch (MongoTimeoutException e) { throw new SiteWhereException("Connection to MongoDB lost.", e); } }
From source file:com.sitewhere.user.persistence.mongodb.MongoUserManagement.java
License:Open Source License
/** * Get the {@link Document} for a User given unique username. * //from w w w . ja v a2 s . c o m * @param username * @return * @throws SiteWhereException */ protected Document getUserDocumentByUsername(String username) throws SiteWhereException { try { MongoCollection<Document> users = getMongoClient().getUsersCollection(); Document query = new Document(MongoUser.PROP_USERNAME, username); return users.find(query).first(); } catch (MongoTimeoutException e) { throw new SiteWhereException("Connection to MongoDB lost.", e); } }
From source file:com.sitewhere.user.persistence.mongodb.MongoUserManagement.java
License:Open Source License
/** * Get the {@link Document} for a GrantedAuthority given unique name. * /*w w w . j a va 2s . c om*/ * @param name * @return * @throws SiteWhereException */ protected Document getGrantedAuthorityDocumentByName(String name) throws SiteWhereException { try { MongoCollection<Document> auths = getMongoClient().getAuthoritiesCollection(); Document query = new Document(MongoGrantedAuthority.PROP_AUTHORITY, name); return auths.find(query).first(); } catch (MongoTimeoutException e) { throw new SiteWhereException("Connection to MongoDB lost.", e); } }
From source file:com.spleefleague.bungee.io.EntityBuilder.java
public static <T extends DBEntity & DBSaveable> void save(T object, MongoCollection<Document> dbcoll, boolean override) { DBEntity dbe = (DBEntity) object;// w w w.j a va 2s.c o m ObjectId _id = dbe.getObjectId(); Document index = null; if (override && _id != null) { index = new Document("_id", _id); } Document dbo = serialize(object); validate(dbo); if (index != null) { dbcoll.updateOne(index, dbo); } else { dbo = (Document) dbo.get("$set"); dbcoll.insertOne(dbo); _id = (ObjectId) dbcoll.find(dbo).first().get("_id"); try { Field _idField = DBEntity.class.getDeclaredField("_id"); _idField.setAccessible(true); _idField.set(dbe, _id); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } }
From source file:com.spleefleague.core.io.EntityBuilder.java
public static <T extends DBEntity & DBSaveable> void save(T object, MongoCollection<Document> dbcoll, boolean override) { if (object == null) { return;/*ww w.j ava 2 s. c o m*/ } DBEntity dbe = (DBEntity) object; ObjectId _id = dbe.getObjectId(); Document index = null; if (override && _id != null) { index = new Document("_id", _id); } Document dbo = serialize(object); validate(dbo); if (index != null) { dbcoll.updateOne(index, dbo); } else { dbo = (Document) dbo.get("$set"); dbcoll.insertOne(dbo); _id = (ObjectId) dbcoll.find(dbo).first().get("_id"); try { Field _idField = DBEntity.class.getDeclaredField("_id"); _idField.setAccessible(true); _idField.set(dbe, _id); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } }
From source file:com.spleefleague.core.utils.DatabaseConnection.java
public static void find(final MongoCollection<Document> dbcoll, final Document query, Consumer<FindIterable<Document>> callback) { Bukkit.getScheduler().runTaskAsynchronously(SpleefLeague.getInstance(), () -> { callback.accept(dbcoll.find(query)); });//from w w w. jav a 2s . c om }
From source file:com.threecrickets.prudence.cache.HazelcastMongoDbMapStore.java
License:LGPL
public V load(K key) { MongoCollection<Document> collection = getCollection(); Document query = new Document(); query.put("_id", key); Document value = collection.find(query).first(); if (value != null) { @SuppressWarnings("unchecked") V fromBinary = (V) fromBinary((byte[]) value.get("value")); return fromBinary; }/*from w w w. j a v a 2 s. c om*/ return null; }
From source file:com.threecrickets.prudence.cache.HazelcastMongoDbMapStore.java
License:LGPL
public Map<K, V> loadAll(Collection<K> keys) { MongoCollection<Document> collection = getCollection(); Document query = new Document(); Document in = new Document(); query.put("_id", in); in.put("$in", keys); HashMap<K, V> map = new HashMap<K, V>(); for (Document value : collection.find(query)) { @SuppressWarnings("unchecked") K key = (K) value.get("_id"); @SuppressWarnings("unchecked") V fromBinary = (V) fromBinary((byte[]) value.get("value")); map.put(key, fromBinary);//from w ww . j ava 2s . co m } return map; }
From source file:com.um.mongodb.converter.EhealthRecordConverter.java
/** * //from www . j a va 2 s . c om * @param ags * @return */ public static int main(String[] ags) { MongoClient client = new MongoClient("localhost", 27017); if (client != null) { System.out.println("success"); } else { System.out.println("failed"); } MongoDatabase database = client.getDatabase("db"); if (database == null) { System.out.println("db is null"); } else { System.out.println("db is not null"); } MongoCollection<Document> collection = database.getCollection("ehealth"); // System.out.println(collection.count()); MongoCursor<Document> cursor = collection .find(new BasicDBObject("ehealthrecord.registrationno", "600025873102")).iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } return 0; }
From source file:com.yahoo.ycsb.db3.MongoDbClient.java
License:Open Source License
/** * Read a record from the database. Each field/value pair from the result will * be stored in a HashMap./* w ww . j a v a2s. co m*/ * * @param table * The name of the table * @param key * The record key of the record to read. * @param fields * The list of fields to read, or null for all of them * @param result * A HashMap of field/value pairs for the result * @return Zero on success, a non-zero error code on error or "not found". */ @Override public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) { try { MongoCollection<Document> collection = database.getCollection(table); Document query = new Document("_id", key); FindIterable<Document> findIterable = collection.find(query); if (fields != null) { Document projection = new Document(); for (String field : fields) { projection.put(field, INCLUDE); } findIterable.projection(projection); } Document queryResult = findIterable.first(); if (queryResult != null) { fillMap(result, queryResult); } return queryResult != null ? Status.OK : Status.NOT_FOUND; } catch (Exception e) { System.err.println(e.toString()); return Status.ERROR; } }