@Override public boolean documentHasId(final Document document) { return document.containsKey(ID_FIELD_NAME); }
String checkFieldExists(Document doc, String field) { if (doc.containsKey(field)) { throw new IllegalArgumentException("Document already contains field : " + field); } return field; }
private List<Document> getColumnMetadata(Document doc) { if (!doc.containsKey(FIELDS_KEY)) { return ImmutableList.of(); } return (List<Document>) doc.get(FIELDS_KEY); }
/** * The initial cursor batch size, if available, otherwise {@literal null}. * * @return the batch size or {@literal null}. * @since 2.0 */ @Nullable public Integer getCursorBatchSize() { if (cursor.filter(val -> val.containsKey(BATCH_SIZE)).isPresent()) { return cursor.get().get(BATCH_SIZE, Integer.class); } return null; }
@Override public ConvertToIndex equal(String name, boolean negate, @Nullable Object value) { if (document.containsKey(name)) { throw new IllegalArgumentException( String.format("Attribute %s is not unique: %s", name, document.get(name))); } document.put(name, value); return this; } }
public Document mapSchema(Document jsonSchema, Class<?> type) { Assert.notNull(jsonSchema, "Schema must not be null!"); Assert.notNull(type, "Type must not be null! Please consider Object.class."); Assert.isTrue(jsonSchema.containsKey($JSON_SCHEMA), () -> String.format("Document does not contain $jsonSchema field. Found %s.", jsonSchema)); if (Object.class.equals(type)) { return new Document(jsonSchema); } return new Document($JSON_SCHEMA, mapSchemaObject(mappingContext.getPersistentEntity(type), jsonSchema.get($JSON_SCHEMA, Document.class))); }
public static List<MongoIndex> parse(ListIndexesIterable<Document> indexes) { ImmutableList.Builder<MongoIndex> builder = ImmutableList.builder(); for (Document index : indexes) { // TODO: v, ns, sparse fields Document key = (Document) index.get("key"); String name = index.getString("name"); boolean unique = index.getBoolean("unique", false); if (key.containsKey("_fts")) { // Full Text Search continue; } builder.add(new MongoIndex(name, parseKey(key), unique)); } return builder.build(); }
/** * The content type of the file * * @return the content type of the file * @deprecated content type information should be stored the metadata document instead. */ @Deprecated public String getContentType() { if (extraElements != null && extraElements.containsKey("contentType")) { return extraElements.getString("contentType"); } else { throw new MongoGridFSException("No contentType data for this GridFS file"); } }
@Override public Object transform(final Object value) { if (value instanceof Document) { Document document = (Document) value; if (document.containsKey("$id") && document.containsKey("$ref")) { return new DBRef((String) document.get("$db"), (String) document.get("$ref"), document.get("$id")); } } return value; }
private boolean containsVersionProperty(Document document, @Nullable MongoPersistentEntity<?> persistentEntity) { if (persistentEntity == null || !persistentEntity.hasVersionProperty()) { return false; } return document.containsKey(persistentEntity.getRequiredIdProperty().getFieldName()); }
private Document mapEnumValuesIfNecessary(Document source) { Document sink = new Document(source); if (source.containsKey(ENUM_FIELD)) { sink.replace(ENUM_FIELD, mapEnumValues(source.get(ENUM_FIELD, Iterable.class))); } return sink; }
@SuppressWarnings("unchecked") private Document mapSchemaObject(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Document source) { Document sink = new Document(source); if (source.containsKey(REQUIRED_FIELD)) { sink.replace(REQUIRED_FIELD, mapRequiredProperties(entity, source.get(REQUIRED_FIELD, Collection.class))); } if (source.containsKey(PROPERTIES_FIELD)) { sink.replace(PROPERTIES_FIELD, mapProperties(entity, source.get(PROPERTIES_FIELD, Document.class))); } mapEnumValuesIfNecessary(sink); return sink; }
@Override public Point convert(Document source) { if (source == null) { return null; } Assert.isTrue(source.keySet().size() == 2, "Source must contain 2 elements"); if (source.containsKey("type")) { return DocumentToGeoJsonPointConverter.INSTANCE.convert(source); } return new Point(toPrimitiveDoubleValue(source.get("x")), toPrimitiveDoubleValue(source.get("y"))); } }
@Override public MappedDocument toMappedDocument(MongoWriter<? super T> writer) { T bean = propertyAccessor.getBean(); Document document = new Document(); writer.write(bean, document); if (document.containsKey(ID_FIELD) && document.get(ID_FIELD) == null) { document.remove(ID_FIELD); } return MappedDocument.of(document); }
private Document getMappedValidator(Validator validator, Class<?> domainType) { Document validationRules = validator.toDocument(); if (validationRules.containsKey("$jsonSchema")) { return schemaMapper.mapSchema(validationRules, domainType); } return queryMapper.getMappedObject(validationRules, mappingContext.getPersistentEntity(domainType)); }
Document getMappedValidator(Validator validator, Class<?> domainType) { Document validationRules = validator.toDocument(); if (validationRules.containsKey("$jsonSchema")) { return schemaMapper.mapSchema(validationRules, domainType); } return queryMapper.getMappedObject(validationRules, mappingContext.getPersistentEntity(domainType)); }
private Document mapProperty(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, String sourceFieldName, Document source) { Document sink = new Document(source); if (entity != null && sink.containsKey(Type.objectType().representation())) { MongoPersistentProperty property = entity.getPersistentProperty(sourceFieldName); if (property != null && property.isEntity()) { sink = mapSchemaObject(mappingContext.getPersistentEntity(property.getActualType()), source); } } return mapEnumValuesIfNecessary(sink); }
private void mapMetaAttributes(Document source, @Nullable MongoPersistentEntity<?> entity, MetaMapping metaMapping) { if (entity == null) { return; } if (entity.hasTextScoreProperty() && !MetaMapping.IGNORE.equals(metaMapping)) { MongoPersistentProperty textScoreProperty = entity.getTextScoreProperty(); if (MetaMapping.FORCE.equals(metaMapping) || (MetaMapping.WHEN_PRESENT.equals(metaMapping) && source.containsKey(textScoreProperty.getFieldName()))) { source.putAll(getMappedTextScoreField(textScoreProperty)); } } }
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { if (dbId != null) { collection.replaceOne(Filters.eq("_id", dbId.get("_id")), dbDoc); } else { if (dbDoc.containsKey("_id") && dbDoc.get("_id") == null) { dbDoc.remove("_id"); } collection.insertOne(dbDoc); } return null; } });
public String ensureIndex(final IndexDefinition indexDefinition) { return execute(collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName))); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }); }