Search in sources :

Example 46 with Document

use of org.bson.Document in project mongo-hadoop by mongodb.

the class UDFTest method testObjectIdToSeconds.

@Test
public void testObjectIdToSeconds() throws IOException, ParseException {
    PigTest.runScript("/pig/oidtoseconds.pig");
    assertEquals(insertedDocuments.size(), OUTPUT_COLLECTION.count());
    Iterator<Document> it = insertedDocuments.iterator();
    for (Document outputDoc : OUTPUT_COLLECTION.find()) {
        int seconds = outputDoc.getInteger("seconds");
        int seconds2 = outputDoc.getInteger("seconds2");
        int expectedSeconds = it.next().getObjectId("_id").getTimestamp();
        assertEquals(expectedSeconds, seconds);
        assertEquals(expectedSeconds, seconds2);
    }
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Example 47 with Document

use of org.bson.Document in project mongo-hadoop by mongodb.

the class UDFTest method testUDFsSchemaless.

@Test
public void testUDFsSchemaless() throws IOException, ParseException {
    // Test that one of our UDFs can work without any schemas being
    // specified. This mostly tests that BSONStorage can infer the type
    // correctly.
    PigTest.runScript("/pig/udfschemaless.pig");
    assertEquals(insertedDocuments.size(), OUTPUT_COLLECTION.count());
    Iterator<Document> it = insertedDocuments.iterator();
    for (Document doc : OUTPUT_COLLECTION.find()) {
        // We don't know what Pig will call the fields that aren't "_id".
        ObjectId expectedId = it.next().getObjectId("_id");
        for (Map.Entry<String, Object> entry : doc.entrySet()) {
            // interested in.
            if ("_id".equals(entry.getKey())) {
                continue;
            }
            assertEquals(expectedId, entry.getValue());
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) Map(java.util.Map) Test(org.junit.Test)

Example 48 with Document

use of org.bson.Document in project mongo-hadoop by mongodb.

the class TablePropertiesTest method setUp.

@Before
public void setUp() {
    MongoClientURI clientURI = new MongoClientURI("mongodb://localhost:27017/mongo_hadoop.tabletest");
    MongoClient client = new MongoClient(clientURI);
    // Seed some documents into MongoDB.
    collection = client.getDatabase(clientURI.getDatabase()).getCollection(clientURI.getCollection());
    ArrayList<Document> documents = new ArrayList<Document>(1000);
    for (int i = 0; i < 1000; ++i) {
        documents.add(new Document("i", i));
    }
    collection.insertMany(documents);
    // Make sure table doesn't exist already.
    dropTable("props_file_test");
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) ArrayList(java.util.ArrayList) Document(org.bson.Document) Before(org.junit.Before)

Example 49 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class CodeWithScopeCodec method decode.

@Override
public CodeWithScope decode(final BsonReader bsonReader, final DecoderContext decoderContext) {
    String code = bsonReader.readJavaScriptWithScope();
    Document scope = documentCodec.decode(bsonReader, decoderContext);
    return new CodeWithScope(code, scope);
}
Also used : CodeWithScope(org.bson.types.CodeWithScope) Document(org.bson.Document)

Example 50 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class GridFSDownloadStreamImpl method checkAndFetchResults.

private void checkAndFetchResults(final int amountRead, final ByteBuffer dst, final SingleResultCallback<Integer> callback) {
    if (currentPosition == fileInfo.getLength() || dst.remaining() == 0) {
        callback.onResult(amountRead, null);
    } else if (hasResultsToProcess()) {
        processResults(amountRead, dst, callback);
    } else if (cursor == null) {
        chunksCollection.find(new Document("files_id", fileInfo.getId()).append("n", new Document("$gte", chunkIndex))).batchSize(batchSize).sort(new Document("n", 1)).batchCursor(new SingleResultCallback<AsyncBatchCursor<Document>>() {

            @Override
            public void onResult(final AsyncBatchCursor<Document> result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else {
                    cursor = result;
                    checkAndFetchResults(amountRead, dst, callback);
                }
            }
        });
    } else {
        cursor.next(new SingleResultCallback<List<Document>>() {

            @Override
            public void onResult(final List<Document> result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else if (result == null || result.isEmpty()) {
                    callback.onResult(null, chunkNotFound(chunkIndex));
                } else {
                    resultsQueue.addAll(result);
                    if (batchSize == 1) {
                        discardCursor();
                    }
                    processResults(amountRead, dst, callback);
                }
            }
        });
    }
}
Also used : AsyncBatchCursor(com.mongodb.async.AsyncBatchCursor) List(java.util.List) Document(org.bson.Document)

Aggregations

Document (org.bson.Document)1104 Test (org.junit.Test)795 ArrayList (java.util.ArrayList)74 Update (org.springframework.data.mongodb.core.query.Update)71 List (java.util.List)55 BsonDocument (org.bson.BsonDocument)53 ObjectId (org.bson.types.ObjectId)41 MongoDatabase (com.mongodb.client.MongoDatabase)40 Query (org.springframework.data.mongodb.core.query.Query)40 BasicDBObject (com.mongodb.BasicDBObject)39 MongoClient (com.mongodb.MongoClient)32 Bson (org.bson.conversions.Bson)32 ReturnDocument (com.mongodb.client.model.ReturnDocument)31 DBObject (com.mongodb.DBObject)27 DBRef (com.mongodb.DBRef)25 UnknownHostException (java.net.UnknownHostException)25 HashMap (java.util.HashMap)24 FullDocument (com.mongodb.client.model.changestream.FullDocument)23 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)21 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)21