org.bson.Document Java Examples
The following examples show how to use
org.bson.Document.
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: DatastoreImpl.java From morphia with Apache License 2.0 | 6 votes |
@Override public <T> T merge(final T entity, final InsertOneOptions options) { final Object id = mapper.getId(entity); if (id == null) { throw new MappingException("Could not get id for " + entity.getClass().getName()); } final Document document = mapper.toDocument(entity); document.remove("_id"); final Query<T> query = (Query<T>) find(entity.getClass()).filter(eq("_id", id)); if (!tryVersionedUpdate(entity, mapper.getCollection(entity.getClass()), options)) { UpdateResult execute = query.update(UpdateOperators.set(entity)) .execute(new UpdateOptions() .clientSession(findSession(options)) .writeConcern(options.writeConcern())); if (execute.getModifiedCount() != 1) { throw new UpdateException("Nothing updated"); } } return query.first(); }
Example #2
Source File: MongoTest.java From game-server with MIT License | 6 votes |
@Ignore @Test public void testInsert() { MongoClientURI connectionString = new MongoClientURI("mongodb://127.0.0.1"); MongoClient mongoClient = new MongoClient(connectionString); MongoDatabase database = mongoClient.getDatabase("lztb_att"); MongoCollection<Document> collection = database.getCollection("test"); Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("info", new Document("x", 203).append("y", 102)); new Document().append("1", 1); collection.insertOne(doc); mongoClient.close(); }
Example #3
Source File: ProfilingWriter.java From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 | 6 votes |
public ProfilingWriter(BlockingQueue<ProfilingEntry> jobQueue) { this.jobQueue = jobQueue; serverDto = ConfigReader.getCollectorServer(); runningSince = new Date(); final MongoDbAccessor mongo = getMongoDbAccessor(); try { final MongoCollection<Document> profileCollection = getProfileCollection(mongo); IndexOptions indexOptions = new IndexOptions(); indexOptions.background(true); LOG.info("Create index {ts:-1, lbl:1} in the background if it does not yet exists"); profileCollection.createIndex(new BasicDBObject("ts",-1).append("lbl", 1), indexOptions); LOG.info("Create index {adr:1, db:1, ts:-1} in the background if it does not yet exists"); profileCollection.createIndex(new BasicDBObject("adr",1).append("db",1).append("ts", -1), indexOptions); LOG.info("ProfilingWriter is ready at {}", serverDto.getHosts()); } catch (MongoException e) { LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e); } }
Example #4
Source File: LumongoIndex.java From lumongo with Apache License 2.0 | 6 votes |
private void storeIndexSettings() { indexLock.writeLock().lock(); try { MongoDatabase db = mongo.getDatabase(mongoConfig.getDatabaseName()); MongoCollection<Document> dbCollection = db.getCollection(indexConfig.getIndexName() + CONFIG_SUFFIX); Document settings = IndexConfigUtil.toDocument(indexConfig); settings.put(MongoConstants.StandardFields._ID, SETTINGS_ID); Document query = new Document(); query.put(MongoConstants.StandardFields._ID, SETTINGS_ID); dbCollection.replaceOne(query, settings, new UpdateOptions().upsert(true)); } finally { indexLock.writeLock().unlock(); } }
Example #5
Source File: ReNounRelationshipAnnotator.java From baleen with Apache License 2.0 | 6 votes |
@Override protected Supplier<Stream<DependencyTreeMatcher>> createPatternsSupplier() throws DependencyParseException { return new Supplier<Stream<DependencyTreeMatcher>>() { MongoDatabase db = mongoResource.getDB(); final MongoCollection<Document> coll = db.getCollection(patternsCollection); @Override public Stream<DependencyTreeMatcher> get() { return StreamSupport.stream(coll.find().spliterator(), false) .map( document -> { try { return DependencyTreeMatcher.create(document.getString(patternField)); } catch (DependencyParseException e) { return null; } }) .filter(Predicates.notNull()); } }; }
Example #6
Source File: HeritDMAdaptor.java From SI with BSD 2-Clause "Simplified" License | 6 votes |
protected Document execute(String deviceId, HashMap<String, String> mos) throws HitDMException { String to = "/hit/openapi/dm/write"; Document content = new Document(); content.put("d", deviceId); List<Document> list = new ArrayList<Document>(); Set<String> set = mos.keySet(); Iterator<String> it = set.iterator(); while (it.hasNext()) { Document e = new Document(); String key = it.next(); String val = mos.get(key); e.put("n", key); e.put("sv", val); list.add(e); } content.put("e", list); return callApi(to, content); }
Example #7
Source File: MongobeeEnvTest.java From mongobee with Apache License 2.0 | 6 votes |
@Test public void shouldRunChangesetWithNullEnvironment() throws Exception { // given runner.setSpringEnvironment(null); runner.setChangeLogsScanPackage(EnvironmentDependentTestResource.class.getPackage().getName()); when(dao.isNewChange(any(ChangeEntry.class))).thenReturn(true); // when runner.execute(); // then long change1 = fakeMongoDatabase.getCollection(CHANGELOG_COLLECTION_NAME) .count(new Document() .append(ChangeEntry.KEY_CHANGEID, "Envtest1") .append(ChangeEntry.KEY_AUTHOR, "testuser")); assertEquals(1, change1); }
Example #8
Source File: PermissionStore.java From EDDI with Apache License 2.0 | 6 votes |
@Override public void copyPermissions(String fromResourceId, String toResourceId) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceNotFoundException { Document permissionsDocument = collection.find(new Document("_id", new ObjectId(fromResourceId))).first(); try { if (permissionsDocument == null) { String message = "Resource 'Permissions' not found. (id=%s)"; message = String.format(message, fromResourceId); throw new IResourceStore.ResourceNotFoundException(message); } permissionsDocument.remove("_id"); Permissions permissions = documentBuilder.build(permissionsDocument, Permissions.class); createPermissions(toResourceId, permissions); } catch (IOException e) { log.debug(e.getLocalizedMessage(), e); throw new IResourceStore.ResourceStoreException("Cannot parse json structure into Permissions entity.", e); } }
Example #9
Source File: PutMongoTest.java From localization_nifi with Apache License 2.0 | 6 votes |
/** * Verifies that 'update' does not insert if 'upsert' if false. * @see #testUpsert() */ @Test public void testUpdateDoesNotInsert() throws Exception { Document doc = DOCUMENTS.get(0); byte[] bytes = documentToByteArray(doc); runner.setProperty(PutMongo.MODE, "update"); runner.enqueue(bytes); runner.run(); runner.assertAllFlowFilesTransferred(PutMongo.REL_SUCCESS, 1); MockFlowFile out = runner.getFlowFilesForRelationship(PutMongo.REL_SUCCESS).get(0); out.assertContentEquals(bytes); // nothing was in collection, so nothing to update since upsert defaults to false assertEquals(0, collection.count()); }
Example #10
Source File: MongoDbIOTest.java From beam with Apache License 2.0 | 5 votes |
@Test public void testSplitIntoBucket() { // a single split should result in two buckets ArrayList<Document> documents = new ArrayList<>(); documents.add(new Document("_id", new ObjectId("52cc8f6254c5317943000005"))); List<BsonDocument> buckets = MongoDbIO.BoundedMongoDbSource.splitKeysToMatch(documents); assertEquals(2, buckets.size()); assertEquals( "{ \"$match\" : { \"_id\" : { \"$lte\" : { \"$oid\" : \"52cc8f6254c5317943000005\" } } } }", buckets.get(0).toString()); assertEquals( "{ \"$match\" : { \"_id\" : { \"$gt\" : { \"$oid\" : \"52cc8f6254c5317943000005\" } } } }", buckets.get(1).toString()); // add more splits and verify the buckets documents.add(new Document("_id", new ObjectId("52cc8f6254c5317943000007"))); documents.add(new Document("_id", new ObjectId("54242e9e54c531ef8800001f"))); buckets = MongoDbIO.BoundedMongoDbSource.splitKeysToMatch(documents); assertEquals(4, buckets.size()); assertEquals( "{ \"$match\" : { \"_id\" : { \"$lte\" : { \"$oid\" : \"52cc8f6254c5317943000005\" } } } }", buckets.get(0).toString()); assertEquals( "{ \"$match\" : { \"_id\" : { \"$gt\" : { \"$oid\" : \"52cc8f6254c5317943000005\" }, \"$lte\" : { \"$oid\" : \"52cc8f6254c5317943000007\" } } } }", buckets.get(1).toString()); assertEquals( "{ \"$match\" : { \"_id\" : { \"$gt\" : { \"$oid\" : \"52cc8f6254c5317943000007\" }, \"$lte\" : { \"$oid\" : \"54242e9e54c531ef8800001f\" } } } }", buckets.get(2).toString()); assertEquals( "{ \"$match\" : { \"_id\" : { \"$gt\" : { \"$oid\" : \"54242e9e54c531ef8800001f\" } } } }", buckets.get(3).toString()); }
Example #11
Source File: DescriptorStore.java From EDDI with Apache License 2.0 | 5 votes |
public DescriptorStore(MongoDatabase database, IPermissionStore permissionStore, IUserStore userStore, IGroupStore groupStore, IDocumentBuilder documentBuilder, Class<T> documentType) { RuntimeUtilities.checkNotNull(database, "database"); RuntimeUtilities.checkNotNull(permissionStore, "permissionStore"); MongoCollection<Document> descriptorCollection = database.getCollection(COLLECTION_DESCRIPTORS); MongoResourceStorage<T> resourceStorage = new MongoResourceStorage<>(database, collectionName, documentBuilder, documentType); this.descriptorResourceStore = new ModifiableHistorizedResourceStore<>(resourceStorage); this.resourceFilter = new ResourceFilter<>(descriptorCollection, descriptorResourceStore, permissionStore, userStore, groupStore, documentBuilder, documentType); descriptorCollection.createIndex(Indexes.ascending(FIELD_RESOURCE), new IndexOptions().unique(true)); }
Example #12
Source File: CASAuthDAO.java From SI with BSD 2-Clause "Simplified" License | 5 votes |
public void deleteAuthInfo(String devId) { MongoCollection<Document> collection = context.getDatabaseManager().getCollection(collectionName); Document authDoc = getDocument(AUTH_KEY_NAME, devId); if(authDoc != null) { collection.deleteOne(authDoc); } }
Example #13
Source File: SavedFieldInfo.java From lumongo with Apache License 2.0 | 5 votes |
public void populate(T newInstance, Document savedDBObject) throws Exception { Object value = savedDBObject.get(fieldName); boolean valuesIsList = value instanceof List; if (valuesIsList) { if (fieldIsList) { field.set(newInstance, new ArrayList<>((List<?>) value)); } else { List<?> valueList = (List<?>) value; if (valueList.size() == 1) { Object first = valueList.iterator().next(); if (first != null) { field.set(newInstance, first); } } else if (valueList.isEmpty()) { } else { throw new Exception("Cannot assign multiple values <" + valueList + "> to field <" + field.getName() + "> with type <" + field.getType() + "> because it is not a list."); } } } else { if (fieldIsList) { if (value != null) { field.set(newInstance, new ArrayList<>(Collections.singletonList(value))); } } else { field.set(newInstance, value); } } }
Example #14
Source File: OldMongoNotebookRepo.java From zeppelin with Apache License 2.0 | 5 votes |
/** * Convert note to document */ private Document noteToDocument(Note note) { // note to JSON String json = note.toJson(); // JSON to document Document doc = Document.parse(json); // set object id as note id doc.put("_id", note.getId()); return doc; }
Example #15
Source File: MongoStorage.java From LuckPerms with MIT License | 5 votes |
private static List<Document> contextSetToDocs(ContextSet contextSet) { List<Document> contexts = new ArrayList<>(contextSet.size()); for (Context e : contextSet) { contexts.add(new Document().append("key", e.getKey()).append("value", e.getValue())); } return contexts; }
Example #16
Source File: MiniTransactionInfo.java From nuls-v2 with MIT License | 5 votes |
public static MiniTransactionInfo toInfo(Document document) { MiniTransactionInfo info = new MiniTransactionInfo(); info.hash = document.getString("_id"); info.type = document.getInteger("type"); info.height = document.getLong("height"); info.createTime = document.getLong("createTime"); info.value = new BigInteger(document.getString("value")); info.status = document.getInteger("status"); info.fee = DocumentTransferTool.toInfo((Document) document.get("fee"), FeeInfo.class); return info; }
Example #17
Source File: MongoTestBase.java From quarkus with Apache License 2.0 | 5 votes |
protected Uni<Void> insertDocs(ReactiveMongoClient mongoClient, String collection, int num) { io.quarkus.mongodb.reactive.ReactiveMongoDatabase database = mongoClient.getDatabase(DATABASE); io.quarkus.mongodb.reactive.ReactiveMongoCollection<Document> mongoCollection = database .getCollection(collection); List<CompletableFuture<InsertOneResult>> list = new ArrayList<>(); for (int i = 0; i < num; i++) { Document doc = createDoc(i); list.add(mongoCollection.insertOne(doc).subscribeAsCompletionStage()); } CompletableFuture<InsertOneResult>[] array = list.toArray(new CompletableFuture[0]); return Uni.createFrom().completionStage(CompletableFuture.allOf(array)); }
Example #18
Source File: DateExpressionTest.java From morphia with Apache License 2.0 | 5 votes |
@Test public void testDateFromParts() { getDs().save(new Sales()); Document result = getDs() .aggregate(Sales.class) .project(of() .include("date", dateFromParts() .year(2017) .month(2) .day(8) .hour(12)) .include("date_iso", dateFromParts() .isoWeekYear(2017) .isoWeek(6) .isoDayOfWeek(3) .hour(12)) .include("date_timezone", dateFromParts() .year(2016) .month(12) .day(31) .hour(23) .minute(46) .second(12) .timezone("America/New_York"))) .execute(Document.class) .next(); result.remove("_id"); assertEquals(parse("{'date': ISODate('2017-02-08T12:00:00Z'), 'date_iso': ISODate('2017-02-08T12:00:00Z')," + "'date_timezone': ISODate('2017-01-01T04:46:12Z')}"), result); }
Example #19
Source File: AggregationTest.java From morphia with Apache License 2.0 | 5 votes |
@Test public void testNullGroupId() { getDs().save(asList(new User("John", new Date()), new User("Paul", new Date()), new User("George", new Date()), new User("Ringo", new Date()))); Aggregation<User> pipeline = getDs() .aggregate(User.class) .group(Group.of() .field("count", sum(value(1)))); assertEquals(Integer.valueOf(4), pipeline.execute(Document.class).tryNext().getInteger("count")); }
Example #20
Source File: Fixture.java From mongo-java-driver-reactivestreams with Apache License 2.0 | 5 votes |
public static void drop(final MongoNamespace namespace) throws Throwable { try { ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>(); getMongoClient().getDatabase(namespace.getDatabaseName()) .runCommand(new Document("drop", namespace.getCollectionName())) .subscribe(subscriber); subscriber.await(20, SECONDS); } catch (MongoCommandException e) { if (!e.getErrorMessage().contains("ns not found")) { throw e; } } }
Example #21
Source File: NodeDaoImpl.java From redtorch with MIT License | 5 votes |
@Override public void deleteNodeByNodeId(Integer nodeId) { if (nodeId == null) { logger.error("根据节点ID删除节点错误,参数nodeId缺失"); throw new IllegalArgumentException("根据节点ID删除节点错误,参数nodeId缺失"); } try { Document filter = new Document(); filter.append("nodeId", nodeId); this.managementDBClient.delete(managementDBName, NODE_COLLECTION_NAME, filter); } catch (IllegalArgumentException e) { logger.error("根据节点ID删除节点错误,节点ID:{}", nodeId, e); } }
Example #22
Source File: MongoUtil.java From render with GNU General Public License v2.0 | 5 votes |
public static void renameCollection(final MongoDatabase database, final String fromName, final String toName) { if (exists(database, fromName)) { final MongoCollection<Document> fromCollection = database.getCollection(fromName); final MongoNamespace toNamespace = new MongoNamespace(database.getName(), toName); fromCollection.renameCollection(toNamespace); LOG.debug("renameCollection: exit, ran {}.renameCollection({})", MongoUtil.fullName(fromCollection), toName); } }
Example #23
Source File: CleanupStrategyProviderIT.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testUsedRowsOnlyCleanupOnClosedConnection() throws Exception { // GIVEN final CleanupStrategyExecutor<MongoDatabase, Document> strategyExecutor = provider.usedRowsOnlyStrategy(); assertThat(strategyExecutor, notNullValue()); mongoClient.close(); // WHEN strategyExecutor.execute(connection, Arrays.asList(initialDataSet)); }
Example #24
Source File: OneM2MDmAdapter.java From SI with BSD 2-Clause "Simplified" License | 5 votes |
protected Document firmwareUpdate(String deviceId, String url, String version, String packageName) throws HitDMException, IOException { String to = "/firmware"; Document content = new Document(); content.put("d", deviceId); content.put("url", url); content.put("version", version); content.put("pkgName", packageName); return callPostApi(to, content); }
Example #25
Source File: MongoDBConnectorSaveTest.java From syndesis with Apache License 2.0 | 5 votes |
@Test public void mongoSaveNewTest() { // When // Given String saveArguments = "{\"id\":11,\"someText\":\"new\"}"; UpdateResult result = template.requestBody("direct:start", saveArguments, UpdateResult.class); // Then Assertions.assertThat(result.getUpsertedId().asNumber().longValue()).isEqualTo(11L); List<Document> docsFound = collection.find(Filters.eq("_id", 11)).into(new ArrayList<>()); Assertions.assertThat(docsFound).hasSize(1); Assertions.assertThat(docsFound.get(0).getString("test")).isEqualTo("new"); }
Example #26
Source File: MongoThingsSearchUpdaterPersistence.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Override public Source<List<Throwable>, NotUsed> purge(final CharSequence namespace) { final Bson filter = thingNamespaceFilter(namespace); final Bson update = new BsonDocument().append(AbstractWriteModel.SET, new BsonDocument().append(FIELD_DELETE_AT, new BsonDateTime(0L))); final UpdateOptions updateOptions = new UpdateOptions().bypassDocumentValidation(true); final WriteModel<Document> writeModel = new UpdateManyModel<>(filter, update, updateOptions); return Source.fromPublisher(collection.bulkWrite(Collections.singletonList(writeModel))) .map(bulkWriteResult -> Collections.<Throwable>emptyList()) .recoverWithRetries(1, new PFBuilder<Throwable, Source<List<Throwable>, NotUsed>>() .matchAny(throwable -> Source.single(Collections.singletonList(throwable))) .build()); }
Example #27
Source File: OneM2MDmController.java From SI with BSD 2-Clause "Simplified" License | 5 votes |
@Override public DeviceInfo getStatus(DeviceInfo devInfo) throws OneM2MException { OneM2MDmAdapter adaptor = new OneM2MDmAdapter(CfgManager.getInstance().getOneM2mAgentAddress()); String deviceId = devInfo.getObjectIDs().get(0); Document nodeDoc = context.getDatabaseManager().getCollection(CfgManager.getInstance().getResourceDatabaseName()) .find(new BasicDBObject(Naming.NODEID_SN, deviceId)).first(); String agentAddress = ""; agentAddress = nodeDoc.getString(Naming.MGMTCLIENTADDRESS); if(agentAddress != null && !agentAddress.equals("")) { adaptor = new OneM2MDmAdapter(agentAddress); } try { Document doc = adaptor.readDeviceStatus(deviceId, "deviceinfo"); devInfo.setFwVersion(doc.getString("fw_version")); devInfo.setSwVersion(doc.getString("os_version")); devInfo.setDeviceLabel(doc.getString("serial")); devInfo.setManufacturer(doc.getString("manufacturer")); devInfo.setModel(doc.getString("model")); } catch (HitDMException e) { throw convertHitDMExToOneM2MEx(e); } catch(IOException ex) { ex.printStackTrace(); return null; } return devInfo; }
Example #28
Source File: MongoCompensableLock.java From ByteTCC with GNU Lesser General Public License v3.0 | 5 votes |
public boolean reExitTransactionInMongoDB(TransactionXid transactionXid, String identifier) { byte[] global = transactionXid.getGlobalTransactionId(); String instanceId = ByteUtils.byteArrayToString(global); try { String application = CommonUtils.getApplication(this.endpoint); String databaseName = application.replaceAll("\\W", "_"); MongoDatabase mdb = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS); Bson condition = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId); Document increases = new Document(); increases.append("times", -1); Document document = new Document(); document.append("$inc", increases); Document target = collection.findOneAndUpdate(condition, document, new FindOneAndUpdateOptions().upsert(true)); Integer times = target == null ? null : target.getInteger("times"); return times == null ? true : times <= 0; } catch (com.mongodb.MongoWriteException error) { logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, error); return true; } catch (RuntimeException rex) { logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, rex); return true; } }
Example #29
Source File: MongoSession.java From presto with Apache License 2.0 | 5 votes |
@VisibleForTesting static Document buildQuery(TupleDomain<ColumnHandle> tupleDomain) { Document query = new Document(); if (tupleDomain.getDomains().isPresent()) { for (Map.Entry<ColumnHandle, Domain> entry : tupleDomain.getDomains().get().entrySet()) { MongoColumnHandle column = (MongoColumnHandle) entry.getKey(); Optional<Document> predicate = buildPredicate(column, entry.getValue()); predicate.ifPresent(query::putAll); } } return query; }
Example #30
Source File: FindOneAndModifyOperation.java From stitch-android-sdk with Apache License 2.0 | 5 votes |
public T execute(final CoreStitchServiceClient service) { final Document args = new Document(); args.put("database", namespace.getDatabaseName()); args.put("collection", namespace.getCollectionName()); args.put("filter", filter); // Send project and sort if they are not null if (project != null) { args.put("projection", project); } if (sort != null) { args.put("sort", sort); } // findOneAndDelete() does not take these arguments if (!methodName.equals("findOneAndDelete")) { args.put("update", update); if (upsert) { args.put("upsert", true); } if (returnNewDocument) { args.put("returnNewDocument", true); } } return service.callFunction(methodName, Collections.singletonList(args), decoder); }