Search in sources :

Example 71 with Document

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

the class QueryAcceptanceTest method shouldUseFriendlyQueryType.

@Test
public void shouldUseFriendlyQueryType() {
    collection.insertOne(new Document("product", "Book").append("numTimesOrdered", "some"));
    collection.insertOne(new Document("product", "CD").append("numTimesOrdered", "6"));
    collection.insertOne(new Document("product", "DVD").append("numTimesOrdered", 9));
    collection.insertOne(new Document("product", "SomethingElse").append("numTimesOrdered", 10));
    collection.insertOne(new Document("product", "VeryPopular").append("numTimesOrdered", 7843273657286478L));
    List<Document> results = new ArrayList<Document>();
    //TODO make BSON type serializable
    Document filter = new Document("$or", asList(new Document("numTimesOrdered", new Document("$type", INT32.getValue())), new Document("numTimesOrdered", new Document("$type", INT64.getValue()))));
    collection.find(filter).sort(new Document("numTimesOrdered", -1)).into(results);
    assertThat(results.size(), is(3));
    assertThat(results.get(0).get("product").toString(), is("VeryPopular"));
    assertThat(results.get(1).get("product").toString(), is("SomethingElse"));
    assertThat(results.get(2).get("product").toString(), is("DVD"));
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test)

Example 72 with Document

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

the class QueryAcceptanceTest method shouldBeAbleToFilterByType.

@Test
public void shouldBeAbleToFilterByType() {
    collection.insertOne(new Document("product", "Book").append("numTimesOrdered", "some"));
    collection.insertOne(new Document("product", "CD").append("numTimesOrdered", "6"));
    collection.insertOne(new Document("product", "DVD").append("numTimesOrdered", 9));
    collection.insertOne(new Document("product", "SomethingElse").append("numTimesOrdered", 10));
    List<Document> results = new ArrayList<Document>();
    collection.find(new Document("numTimesOrdered", new Document("$type", 16))).sort(new Document("numTimesOrdered", -1)).into(results);
    assertThat(results.size(), is(2));
    assertThat(results.get(0).get("product").toString(), is("SomethingElse"));
    assertThat(results.get(1).get("product").toString(), is("DVD"));
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test)

Example 73 with Document

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

the class QueryAcceptanceTest method shouldBeAbleToSortAscending.

@Test
public void shouldBeAbleToSortAscending() {
    collection.insertOne(new Document("product", "Book"));
    collection.insertOne(new Document("product", "DVD"));
    collection.insertOne(new Document("product", "CD"));
    List<Document> results = new ArrayList<Document>();
    collection.find().sort(new Document("product", 1)).into(results);
    assertThat(results.size(), is(3));
    assertThat(results.get(0).get("product").toString(), is("Book"));
    assertThat(results.get(1).get("product").toString(), is("CD"));
    assertThat(results.get(2).get("product").toString(), is("DVD"));
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test)

Example 74 with Document

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

the class QueryAcceptanceTest method shouldBeAbleToQueryTypedCollectionWithDocument.

@Test
public void shouldBeAbleToQueryTypedCollectionWithDocument() {
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new PersonCodecProvider()));
    MongoCollection<Person> collection = database.getCollection(getCollectionName(), Person.class).withCodecRegistry(codecRegistry);
    collection.insertOne(new Person("Bob"));
    MongoCursor<Person> results = collection.find(new Document("name", "Bob")).iterator();
    assertThat(results.next().name, is("Bob"));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) Document(org.bson.Document) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 75 with Document

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

the class DBCollectionTest method shouldAcceptDocumentsWithAllValidValueTypes.

@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
    BasicDBObject doc = new BasicDBObject();
    doc.append("_id", new ObjectId());
    doc.append("bool", true);
    doc.append("int", 3);
    doc.append("short", (short) 4);
    doc.append("long", 5L);
    doc.append("str", "Hello MongoDB");
    doc.append("float", 6.0f);
    doc.append("double", 1.1);
    doc.append("date", new Date());
    doc.append("ts", new BSONTimestamp(5, 1));
    doc.append("pattern", Pattern.compile(".*"));
    doc.append("minKey", new MinKey());
    doc.append("maxKey", new MaxKey());
    doc.append("js", new Code("code"));
    doc.append("jsWithScope", new CodeWScope("code", new BasicDBObject()));
    doc.append("null", null);
    doc.append("uuid", UUID.randomUUID());
    doc.append("db ref", new com.mongodb.DBRef("test", new ObjectId()));
    doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
    doc.append("byte array", new byte[] { 1, 2, 3 });
    doc.append("int array", new int[] { 4, 5, 6 });
    doc.append("list", asList(7, 8, 9));
    doc.append("doc list", asList(new Document("x", 1), new Document("x", 2)));
    collection.insert(doc);
    DBObject found = collection.findOne();
    assertNotNull(found);
    assertEquals(ObjectId.class, found.get("_id").getClass());
    assertEquals(Boolean.class, found.get("bool").getClass());
    assertEquals(Integer.class, found.get("int").getClass());
    assertEquals(Integer.class, found.get("short").getClass());
    assertEquals(Long.class, found.get("long").getClass());
    assertEquals(String.class, found.get("str").getClass());
    assertEquals(Double.class, found.get("float").getClass());
    assertEquals(Double.class, found.get("double").getClass());
    assertEquals(Date.class, found.get("date").getClass());
    assertEquals(BSONTimestamp.class, found.get("ts").getClass());
    assertEquals(Pattern.class, found.get("pattern").getClass());
    assertEquals(MinKey.class, found.get("minKey").getClass());
    assertEquals(MaxKey.class, found.get("maxKey").getClass());
    assertEquals(Code.class, found.get("js").getClass());
    assertEquals(CodeWScope.class, found.get("jsWithScope").getClass());
    assertNull(found.get("null"));
    assertEquals(UUID.class, found.get("uuid").getClass());
    assertEquals(DBRef.class, found.get("db ref").getClass());
    assertEquals(Binary.class, found.get("binary").getClass());
    assertEquals(byte[].class, found.get("byte array").getClass());
    assertTrue(found.get("int array") instanceof List);
    assertTrue(found.get("list") instanceof List);
    assertTrue(found.get("doc list") instanceof List);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Binary(org.bson.types.Binary) Test(org.junit.Test)

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