com.mongodb.Block Java Examples

The following examples show how to use com.mongodb.Block. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void logicalOr() {

	// @begin: logical-or
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find(new Document("$or",
		asList(new Document("cuisine", "Italian"), new Document("address.zipcode", "10075"))));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(or(eq("cuisine", "Italian"), eq("address.zipcode", "10075")));
	// @code: end

	// @end: logical-or
}
 
Example #2
Source File: MongoDatabaseList.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoClient client = getMongoClient( _session, argStruct );

	try {
		cfArrayData arr = cfArrayData.createArray( 1 );

		client.listDatabaseNames().forEach( new Block<String>() {

			@Override
			public void apply( final String st ) {
				try {
					arr.addElement( new cfStringData( st ) );
				} catch ( cfmRunTimeException e ) {}
			}
		} );

		return arr;
	} catch ( MongoException me ) {
		throwException( _session, me.getMessage() );
		return null;
	}
}
 
Example #3
Source File: MongoSplitHandler.java    From DBus with Apache License 2.0 6 votes vote down vote up
/**
 * 查询每个shard 的主库地址
 */
private Map<String, String> getShardedUrls(MongoManager mongoManager, String username, String password) {
    FindIterable<Document> documents = mongoManager.find("config", "shards");
    Map<String, String> shardsUrlMap = new HashMap<>();
    documents.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {
            //格式:shard1/localhost:27017,localhost:27018,localhost:27019
            String[] shardHosts = document.getString("host").split("/");
            String shardName = shardHosts[0];
            String url = shardHosts[1];

            shardsUrlMap.put(shardName, url);
        }
    });
    return shardsUrlMap;
}
 
Example #4
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryEmbeddedDocument() {
	// @begin: query-embedded-document
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants")
		.find(new Document("address.zipcode", "10075"));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(eq("address.zipcode", "10075"));
	// @code: end
	// @end: query-embedded-document
}
 
Example #5
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
NamespaceSynchronizationConfig(
    final MongoCollection<NamespaceSynchronizationConfig> namespacesColl,
    final MongoCollection<CoreDocumentSynchronizationConfig> docsColl,
    final MongoNamespace namespace
) {
  this.namespacesColl = namespacesColl;
  this.docsColl = docsColl;
  this.namespace = namespace;
  this.syncedDocuments = new ConcurrentHashMap<>();
  this.nsLock = new ReentrantReadWriteLock();

  // Fill from db
  final BsonDocument docsFilter = new BsonDocument();
  docsFilter.put(
      CoreDocumentSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD,
      new BsonString(namespace.toString()));
  docsColl.find(docsFilter, CoreDocumentSynchronizationConfig.class)
      .forEach(new Block<CoreDocumentSynchronizationConfig>() {
        @Override
        public void apply(@Nonnull final CoreDocumentSynchronizationConfig docConfig) {
          syncedDocuments.put(docConfig.getDocumentId(), new CoreDocumentSynchronizationConfig(
              docsColl,
              docConfig));
        }
      });
}
 
Example #6
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryFieldInArray() {
	// @begin: query-field-in-array
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find(new Document("grades.grade", "B"));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document);
		}
	});
	// @code: end

	// @pre: To simplify building queries the Java driver provides static helpers
	// @code: start
	db.getCollection("restaurants").find(eq("grades.grade", "B"));
	// @code: end
	// @end: query-field-in-array
}
 
Example #7
Source File: QueryPrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void queryAll() {
	// @begin: query-all
	// @code: start
	FindIterable<Document> iterable = db.getCollection("restaurants").find();
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document.
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document.toJson());
		}
	});
	// @code: end
	// @end: query-all
}
 
Example #8
Source File: MongoCollectionList.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoClient client	= getMongoClient( _session, argStruct );
	
	try{
		cfArrayData	arr	= cfArrayData.createArray(1);
		
		client.listDatabaseNames().forEach( new Block<String>(){

			@Override
			public void apply( String dbName ) {
				try {
					arr.addElement( new cfStringData( dbName ) );
				} catch ( cfmRunTimeException e ) {}
			}
			
		});
		
		
		return arr;
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example #9
Source File: MongoCollectionImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Success> dropIndex(final String indexName) {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<Success>>() {
        @Override
        public void apply(final SingleResultCallback<Success> callback) {
            wrapped.dropIndex(indexName, voidToSuccessCallback(callback));
        }
    }), observableAdapter);
}
 
Example #10
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<String> createIndex(final ClientSession clientSession, final Bson key, final IndexOptions options) {
    return new ObservableToPublisher<String>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<String>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<String> callback) {
                    wrapped.createIndex(clientSession.getWrapped(), key, options, callback);
                }
            }));
}
 
Example #11
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<TDocument> findOneAndDelete(final Bson filter, final FindOneAndDeleteOptions options) {
    return new ObservableToPublisher<TDocument>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<TDocument>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<TDocument> callback) {
                    wrapped.findOneAndDelete(filter, options, callback);
                }
            }));
}
 
Example #12
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<UpdateResult> updateOne(final Bson filter, final Bson update, final UpdateOptions options) {
    return new ObservableToPublisher<UpdateResult>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<UpdateResult>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<UpdateResult> callback) {
                    wrapped.updateOne(filter, update, options, callback);
                }
            }));
}
 
Example #13
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> dropIndex(final Bson keys, final DropIndexOptions dropIndexOptions) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.dropIndex(keys, dropIndexOptions, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #14
Source File: GridFSBucketImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Success> uploadFromStream(final BsonValue id, final String filename, final AsyncInputStream source,
                                           final GridFSUploadOptions options) {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<Success>>() {
        @Override
        public void apply(final SingleResultCallback<Success> callback) {
            wrapped.uploadFromStream(id, filename, toCallbackAsyncInputStream(source), options, voidToSuccessCallback(callback));
        }
    }), observableAdapter);
}
 
Example #15
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<UpdateResult> updateOne(final ClientSession clientSession, final Bson filter, final List<? extends Bson> update,
                                         final UpdateOptions options) {
    return new ObservableToPublisher<UpdateResult>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<UpdateResult>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<UpdateResult> callback) {
                    wrapped.updateOne(clientSession.getWrapped(), filter, update, options, callback);
                }
            }));
}
 
Example #16
Source File: GridFSBucketImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<ObjectId> uploadFromStream(final String filename, final AsyncInputStream source) {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<ObjectId>>() {
        @Override
        public void apply(final SingleResultCallback<ObjectId> callback) {
            wrapped.uploadFromStream(filename, toCallbackAsyncInputStream(source), callback);
        }
    }), observableAdapter);
}
 
Example #17
Source File: GridFSBucketImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<ObjectId> uploadFromStream(final String filename, final AsyncInputStream source, final GridFSUploadOptions options) {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<ObjectId>>() {
        @Override
        public void apply(final SingleResultCallback<ObjectId> callback) {
            wrapped.uploadFromStream(filename, toCallbackAsyncInputStream(source), options, callback);
        }
    }), observableAdapter);
}
 
Example #18
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Long> downloadToStream(final BsonValue id,
                                        final com.mongodb.reactivestreams.client.gridfs.AsyncOutputStream destination) {
    return new ObservableToPublisher<Long>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Long>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Long> callback) {
                    wrapped.downloadToStream(id, toCallbackAsyncOutputStream(destination), callback);
                }
            }));
}
 
Example #19
Source File: GridFSFindObservableImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<GridFSFile> first() {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<GridFSFile>>(){
        @Override
        public void apply(final SingleResultCallback<GridFSFile> callback) {
            wrapped.first(callback);
        }
    }), observableAdapter);
}
 
Example #20
Source File: MongoCollectionImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Success> drop() {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<Success>>() {
        @Override
        public void apply(final SingleResultCallback<Success> callback) {
            wrapped.drop(voidToSuccessCallback(callback));
        }
    }), observableAdapter);
}
 
Example #21
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<UpdateResult> updateMany(final Bson filter, final List<? extends Bson> update, final UpdateOptions options) {
    return new ObservableToPublisher<UpdateResult>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<UpdateResult>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<UpdateResult> callback) {
                    wrapped.updateMany(filter, update, options, callback);
                }
            }));
}
 
Example #22
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<UpdateResult> updateMany(final Bson filter, final Bson update, final UpdateOptions options) {
    return new ObservableToPublisher<UpdateResult>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<UpdateResult>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<UpdateResult> callback) {
                    wrapped.updateMany(filter, update, options, callback);
                }
            }));
}
 
Example #23
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
@SuppressWarnings("deprecation")
public Publisher<Long> count(final ClientSession clientSession, final Bson filter, final CountOptions options) {
    return new ObservableToPublisher<Long>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Long>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Long> callback) {
                    wrapped.count(clientSession.getWrapped(), filter, options, callback);
                }
            }));
}
 
Example #24
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<DeleteResult> deleteOne(final ClientSession clientSession, final Bson filter, final DeleteOptions options) {
    return new ObservableToPublisher<DeleteResult>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<DeleteResult>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<DeleteResult> callback) {
                    wrapped.deleteOne(clientSession.getWrapped(), filter, options, callback);
                }
            }));
}
 
Example #25
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<String> createIndexes(final ClientSession clientSession, final List<IndexModel> indexes,
                                       final CreateIndexOptions createIndexOptions) {
    return new ObservableToPublisher<String>(com.mongodb.async.client.Observables.observeAndFlatten(
            new Block<com.mongodb.async.SingleResultCallback<List<String>>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<List<String>> callback) {
                    wrapped.createIndexes(clientSession.getWrapped(), indexes, createIndexOptions, callback);
                }
            }));
}
 
Example #26
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> insertOne(final TDocument document, final InsertOneOptions options) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.insertOne(document, options, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #27
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<TDocument> findOneAndDelete(final ClientSession clientSession, final Bson filter,
                                             final FindOneAndDeleteOptions options) {
    return new ObservableToPublisher<TDocument>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<TDocument>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<TDocument> callback) {
                    wrapped.findOneAndDelete(clientSession.getWrapped(), filter, options, callback);
                }
            }));
}
 
Example #28
Source File: MongoCollectionImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<DeleteResult> deleteMany(final Bson filter) {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<DeleteResult>>() {
        @Override
        public void apply(final SingleResultCallback<DeleteResult> callback) {
            wrapped.deleteMany(filter, callback);
        }
    }), observableAdapter);
}
 
Example #29
Source File: MongoCollectionImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<DeleteResult> deleteOne(final Bson filter, final DeleteOptions options) {
    return RxObservables.create(Observables.observe(new Block<SingleResultCallback<DeleteResult>>() {
        @Override
        public void apply(final SingleResultCallback<DeleteResult> callback) {
            wrapped.deleteOne(filter, options, callback);
        }
    }), observableAdapter);
}
 
Example #30
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<TDocument> findOneAndUpdate(final Bson filter, final List<? extends Bson> update,
                                             final FindOneAndUpdateOptions options) {
    return new ObservableToPublisher<TDocument>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<TDocument>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<TDocument> callback) {
                    wrapped.findOneAndUpdate(filter, update, options, callback);
                }
            }));
}