Skip to content

Instantly share code, notes, and snippets.

@jyemin
Created January 28, 2016 21:04
Show Gist options
  • Select an option

  • Save jyemin/a453b1d0f2ed6d1eba9b to your computer and use it in GitHub Desktop.

Select an option

Save jyemin/a453b1d0f2ed6d1eba9b to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBObjectCodecProvider;
import com.mongodb.DBRefCodecProvider;
import com.mongodb.ServerAddress;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.connection.ClusterSettings;
import org.bson.codecs.configuration.CodecRegistries;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import static java.util.Collections.singletonList;
public class Morphia829 {
public static void main(String[] args) throws InterruptedException {
MongoClientSettings settings =
MongoClientSettings.builder()
.clusterSettings(ClusterSettings.builder().hosts(singletonList(new ServerAddress())).build())
.codecRegistry(com.mongodb.MongoClient.getDefaultCodecRegistry())
.build();
MongoClient client = MongoClients.create(settings);
MongoCollection<DBObject> collection = client.getDatabase("test").getCollection("test", DBObject.class);
CountDownLatch latch = new CountDownLatch(1);
collection.insertOne(new BasicDBObject("x", 1), (result, t) -> latch.countDown());
latch.await();
CountDownLatch latch2 = new CountDownLatch(1);
collection.find().first((result, t) -> {
System.out.println(result);
latch2.countDown();
});
latch2.await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment