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 Project: baleen Author: dstl File: ReNounRelationshipAnnotator.java License: 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 #2
Source Project: SI Author: iotoasis File: HeritDMAdaptor.java License: 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 #3
Source Project: game-server Author: jzyong File: MongoTest.java License: 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 #4
Source Project: morphia Author: MorphiaOrg File: DatastoreImpl.java License: 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 #5
Source Project: mongodb-slow-operations-profiler Author: idealo File: ProfilingWriter.java License: 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 #6
Source Project: mongobee Author: mongobee File: MongobeeEnvTest.java License: 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 #7
Source Project: EDDI Author: labsai File: PermissionStore.java License: 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 #8
Source Project: localization_nifi Author: wangrenlei File: PutMongoTest.java License: 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 #9
Source Project: lumongo Author: lumongo File: LumongoIndex.java License: 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 #10
Source Project: render Author: saalfeldlab File: RenderDao.java License: GNU General Public License v2.0 | 5 votes |
/** * Saves the specified tile spec to the database. * * @param stackId stack identifier. * @param tileSpec specification to be saved. * * @throws IllegalArgumentException * if any required parameters or transform spec references are missing. */ void saveTileSpec(final StackId stackId, final TileSpec tileSpec) throws IllegalArgumentException { MongoUtil.validateRequiredParameter("stackId", stackId); MongoUtil.validateRequiredParameter("tileSpec", tileSpec); MongoUtil.validateRequiredParameter("tileSpec.tileId", tileSpec.getTileId()); final MongoCollection<Document> tileCollection = getTileCollection(stackId); final String context = "tile spec with id '" + tileSpec.getTileId(); validateTransformReferences(context, stackId, tileSpec.getTransforms()); final Document query = new Document(); query.put("tileId", tileSpec.getTileId()); final Document tileSpecObject = Document.parse(tileSpec.toJson()); final UpdateResult result = tileCollection.replaceOne(query, tileSpecObject, MongoUtil.UPSERT_OPTION); LOG.debug("saveTileSpec: {}.{},({}), upsertedId is {}", MongoUtil.fullName(tileCollection), MongoUtil.action(result), query.toJson(), result.getUpsertedId()); }
Example #11
Source Project: SI Author: iotoasis File: RemoteCSEDAO.java License: BSD 2-Clause "Simplified" License | 5 votes |
public boolean checkIfRegistered(String cseId, CSE_TYPE cseType) { List<KeyValue> kv = new ArrayList<KeyValue>(); kv.add(new KeyValue(CSEID_KEY, cseId)); kv.add(new KeyValue(RESTYPE_KEY, RESOURCE_TYPE.REMOTE_CSE.Value())); if (cseType != null) { kv.add(new KeyValue(CSETYPE_KEY, cseType.Value())); } List<Document> doc = getDocuments(kv, RESOURCE_TYPE.REMOTE_CSE, null, true, 1); return doc != null && doc.size() > 0; }
Example #12
Source Project: rya Author: apache File: DocumentVisibilityAdapter.java License: Apache License 2.0 | 5 votes |
/** * Serializes a document visibility expression byte array to a MongoDB * {@link Document}. * @param expression the document visibility expression byte array to be * serialized. * @return The MongoDB {@link Document}. */ public static Document toDocument(final byte[] expression) { DocumentVisibility dv; if (expression == null) { dv = MongoDbRdfConstants.EMPTY_DV; } else { dv = new DocumentVisibility(expression); } return toDocument(dv); }
Example #13
Source Project: MongoDb-Sink-Connector Author: RADAR-base File: MongoWrapper.java License: Apache License 2.0 | 5 votes |
/** * Store a document in MongoDB. If the document has an ID, it will replace existing * documents with that ID. If it does not, it will be inserted and MongoDB will assign it with * a unique ID. * * @param topic Kafka topic that the document belongs to * @param doc MongoDB document * @throws MongoException if the document could not be stored. */ public void store(String topic, Document doc) throws MongoException { MongoCollection<Document> collection = getCollection(topic); Object mongoId = doc.get(MONGO_ID_KEY); if (mongoId != null) { collection.replaceOne(eq(MONGO_ID_KEY, mongoId), doc, UPDATE_UPSERT); } else { collection.insertOne(doc); } }
Example #14
Source Project: mongodb-slow-operations-profiler Author: idealo File: MongoDbAccessor.java License: GNU Affero General Public License v3.0 | 5 votes |
private void find(String dbName, String collName, int limit){ final MongoIterable<Document> res = getMongoDatabase(dbName).getCollection(collName) .find() .limit(limit); if(res != null){ for(Document doc : res){ LOG.info("doc: {}", JSON.serialize(doc)); } } }
Example #15
Source Project: Mongodb-WeAdmin Author: qinxuewu File: MongoSdkBase.java License: Apache License 2.0 | 5 votes |
/*** * 插入单条记录 * @param table 表连接 * @param obj 单条数据 * obj double 处理不规范 * @return */ public String insertOne(MongoCollection table, Object obj) { if (obj == null) {return null;} Document docine =Document.parse(diyObjectIdToJson(obj)); docine.remove("_id"); docine.put("_id", new ObjectId().toString()); table.insertOne(docine); return docine.get("_id").toString(); }
Example #16
Source Project: immutables Author: immutables File: JacksonCodecsTest.java License: Apache License 2.0 | 5 votes |
/** * Reading directly {@link Document} */ @Test public void document() throws IOException { final CodecRegistry registry = JacksonCodecs.registryFromMapper(mapper); Document expected = new Document("a", 1); Document actual= registry.get(Document.class).decode(new BsonDocumentReader(expected.toBsonDocument(BsonDocument.class, registry)), DecoderContext.builder().build()); check(actual).is(expected); }
Example #17
Source Project: nifi Author: apache File: PutMongoIT.java License: Apache License 2.0 | 5 votes |
private void testUpdateFullDocument(TestRunner runner) { Document document = new Document() .append("name", "John Smith") .append("department", "Engineering"); collection.insertOne(document); String updateBody = "{\n" + "\t\"name\": \"John Smith\",\n" + "\t\"department\": \"Engineering\",\n" + "\t\"contacts\": {\n" + "\t\t\"phone\": \"555-555-5555\",\n" + "\t\t\"email\": \"[email protected]\",\n" + "\t\t\"twitter\": \"@JohnSmith\"\n" + "\t}\n" + "}"; runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_DOC); runner.setProperty(PutMongo.MODE, PutMongo.MODE_UPDATE); runner.setValidateExpressionUsage(true); runner.enqueue(updateBody); runner.run(); runner.assertTransferCount(PutMongo.REL_FAILURE, 0); runner.assertTransferCount(PutMongo.REL_SUCCESS, 1); MongoCursor<Document> cursor = collection.find(document).iterator(); Document found = cursor.next(); Assert.assertEquals(found.get("name"), document.get("name")); Assert.assertEquals(found.get("department"), document.get("department")); Document contacts = (Document)found.get("contacts"); Assert.assertNotNull(contacts); Assert.assertEquals(contacts.get("twitter"), "@JohnSmith"); Assert.assertEquals(contacts.get("email"), "[email protected]"); Assert.assertEquals(contacts.get("phone"), "555-555-5555"); Assert.assertEquals(collection.count(document), 1); }
Example #18
Source Project: baleen Author: dstl File: MalletClassifierTrainer.java License: Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private Optional<String> getLabel(Document document) { String label = null; try { label = ((List<String>) document.get(labelField)).get(0); } catch (NullPointerException | ClassCastException e) { label = document.getString(labelField); } return Optional.ofNullable(label); }
Example #19
Source Project: edison-microservice Author: otto-de File: MongoStatusDetailIndicatorTest.java License: Apache License 2.0 | 5 votes |
@Test public void shouldReturnErrorStatusOnAnyException() { //given when(mongoDatabase.runCommand(new Document().append("ping", 1))).thenThrow(new MongoException("SomeException")); //when final StatusDetail statusDetail = testee.statusDetails().get(0); //then assertThat(statusDetail.getStatus(), is(ERROR)); assertThat(statusDetail.getMessage(), containsString("Exception during database check")); }
Example #20
Source Project: nuls-v2 Author: nuls-io File: DocumentTransferTool.java License: MIT License | 5 votes |
public static <T> T toInfo(Document document, String _id, Class<T> clazz) { if (null == document) { return null; } try { T instance = clazz.getDeclaredConstructor().newInstance(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if ("isNew".equals(field.getName())) { continue; } if (_id.equals(field.getName())) { field.set(instance, document.get("_id")); } else if (!document.containsKey(field.getName())) { continue; } else if (field.getType().getName().equals("java.math.BigInteger")) { field.set(instance, new BigInteger(document.get(field.getName()).toString())); } else { field.set(instance, document.get(field.getName())); } } return instance; } catch (Exception e) { LoggerUtil.commonLog.error(e); throw new NulsRuntimeException(ApiErrorCode.DATA_PARSE_ERROR, "Document to Model fail"); } }
Example #21
Source Project: stitch-android-sdk Author: mongodb File: FindOneAndModifyOperation.java License: 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); }
Example #22
Source Project: presto Author: prestosql File: MongoSession.java License: 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 #23
Source Project: ByteTCC Author: liuyangming File: MongoCompensableLock.java License: 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 #24
Source Project: SI Author: iotoasis File: OneM2MDmController.java License: 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 #25
Source Project: ditto Author: eclipse File: MongoThingsSearchUpdaterPersistence.java License: 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 #26
Source Project: mongo-java-driver-reactivestreams Author: mongodb File: Fixture.java License: 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 #27
Source Project: syndesis Author: syndesisio File: MongoDBConnectorSaveTest.java License: 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 #28
Source Project: SI Author: iotoasis File: OneM2MDmAdapter.java License: 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 #29
Source Project: jpa-unit Author: dadrus File: CleanupStrategyProviderIT.java License: 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 #30
Source Project: EDDI Author: labsai File: DescriptorStore.java License: 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)); }