private void addCodecs() { addCodec(new BinaryCodec()); addCodec(new BooleanCodec()); addCodec(new ByteCodec()); addCodec(new ShortCodec()); addCodec(new CharacterCodec()); addCodec(new FloatCodec()); addCodec(new DoubleCodec()); addCodec(new IntegerCodec()); addCodec(new LongCodec()); addCodec(new MinKeyCodec()); addCodec(new MaxKeyCodec()); addCodec(new CodeCodec()); addCodec(new ObjectIdCodec()); addCodec(new StringCodec()); addCodec(new SymbolCodec()); addCodec(new UuidCodec()); addCodec(new PatternCodec()); addCodec(new ByteArrayCodec()); addCodec(new AtomicBooleanCodec()); addCodec(new AtomicIntegerCodec()); addCodec(new AtomicLongCodec()); addCodec(new DateCodec()); addCodec(new InstantCodec()); addCodec(new LocalDateCodec()); addCodec(new LocalDateTimeCodec()); addCodec(new ZonedDateTimeCodec()); addCodec(new BooleanArrayCodec()); addCodec(new CharArrayCodec()); addCodec(new ShortArrayCodec()); addCodec(new IntArrayCodec()); addCodec(new LongArrayCodec()); addCodec(new FloatArrayCodec()); addCodec(new DoubleArrayCodec()); }
public void testDriverStatus() { CodecRegistry codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromCodecs(new UuidCodec( UuidRepresentation.STANDARD)), MongoClient .getDefaultCodecRegistry()); mongoClient.getDatabase("lech_rent").drop(); MongoDatabase rent = mongoClient.getDatabase("lech_rent") .withCodecRegistry(codecRegistry); // rent.createCollection("driver_status", new CreateCollectionOptions() // .capped(true).sizeInBytes(0x100000)); MongoCollection<Document> status = rent.getCollection("driver_status"); status.deleteMany(Filters.eq("mobile", "18616020610")); if (status.count() == 0) { } status.createIndex(new Document("mobile", "text")); // status.createIndex(new Document("no", "text")); for (final Document index : status.listIndexes()) { System.out.println(index.toJson()); } Document doc = new Document("loc", new Document("type", "Point").append("coordinates", Arrays.asList(-73.97, 40.77))).append("no", "dno") .append("usrImg", "/usr/driver.png") .append("mobile", "18616020610").append("status", 7) .append("car", new Document("no", "A00001")); status.insertOne(doc); // status.createIndex(keys); doc = status.find(Filters.eq("mobile", "18616020610")).first(); System.out.println(doc.get("loc", Document.class).get("coordinates")); System.out.println(doc.get("loc", Document.class).get("coordinates", ArrayList.class)); System.out.println(doc.get("car", Document.class)); // System.out.println(doc.get("loc", Document.class)); // UpdateResult updateResult = status.updateOne(Filters.eq("mobile", // "18616020610"), new Document("$set", new Document("car", // new Document("no", "A00002")))); doc = status.find(Filters.eq("mobile", "18616020610")).first(); System.out.println(doc.get("car", Document.class)); // updateResult = status.updateMany(Filters.lt("i", 100), new Document( // "$inc", new Document("i", 100))); // System.out.println(updateResult.getModifiedCount()); // DeleteResult deleteResult = status.deleteOne(Filters.eq("i", 110)); // System.out.println(deleteResult.getDeletedCount()); // 2. Ordered bulk operation - order is guarenteed // status.bulkWrite(Arrays.asList(new InsertOneModel<>(new // Document("_id", // 4)), new InsertOneModel<>(new Document("_id", 5)), // new InsertOneModel<>(new Document("_id", 6)), // new UpdateOneModel<>(new Document("_id", 1), new Document( // "$set", new Document("x", 2))), new DeleteOneModel<>( // new Document("_id", 2)), // new ReplaceOneModel<>(new Document("_id", 3), new Document( // "_id", 3).append("x", 4)))); // 2. Unordered bulk operation - no guarantee of order of operation // status.bulkWrite(Arrays.asList(new InsertOneModel<>(new // Document("_id", // 4)), new InsertOneModel<>(new Document("_id", 5)), // new InsertOneModel<>(new Document("_id", 6)), // new UpdateOneModel<>(new Document("_id", 1), new Document( // "$set", new Document("x", 2))), new DeleteOneModel<>( // new Document("_id", 2)), // new ReplaceOneModel<>(new Document("_id", 3), new Document( // "_id", 3).append("x", 4))), new BulkWriteOptions() // .ordered(false)); }