@Override public Set<String> keySet() { Set<String> keys = new HashSet<String>(); keys.addAll(VALID_FIELDS); keys.addAll(extra.keySet()); return keys; }
private static boolean isValueAValidGeoQuery(final Object value) { if (value instanceof DBObject) { String key = ((DBObject) value).keySet().iterator().next(); return key.equals("$box") || key.equals("$center") || key.equals("$centerSphere") || key.equals("$polygon"); } return false; }
private DBObject currentObjectNonNull(final DBObject newCurrentObject){ currentObject = newCurrentObject; numSeen++; DBObject projection = findOptions.getProjection(); if (projection != null && !(projection.keySet().isEmpty())) { currentObject.markAsPartialObject(); } return newCurrentObject; }
/** * Creates a {@code DBObject} query to be used for the driver's find operations * * @return {@code this} * @throws RuntimeException if a key does not have a matching operand */ public DBObject get() { for (final String key : _query.keySet()) { if (_query.get(key) instanceof NullObject) { throw new QueryBuilderException("No operand for key:" + key); } } return _query; }
/** * Determines the read preference that should be used for the given command. * * @param command the {@link DBObject} representing the command * @param requestedPreference the preference requested by the client. * @return the read preference to use for the given command. It will never return {@code null}. * @see com.mongodb.ReadPreference */ ReadPreference getCommandReadPreference(final DBObject command, @Nullable final ReadPreference requestedPreference) { String comString = command.keySet().iterator().next().toLowerCase(); boolean primaryRequired = !OBEDIENT_COMMANDS.contains(comString); if (primaryRequired) { return ReadPreference.primary(); } else if (requestedPreference == null) { return ReadPreference.primary(); } else { return requestedPreference; } }
private boolean filterMatch(DBObject filter, DBObject object) { for (String key : filter.keySet()) { if (!object.containsField(key)) { return false; } if (!filter.get(key).equals(object.get(key))) { return false; } } return true; }
@Override public void serialize(final Object obj, final StringBuilder buf) { boolean first = true; buf.append("{ "); DBObject dbo = (DBObject) obj; String name; for (final String s : dbo.keySet()) { name = s; if (first) { first = false; } else { buf.append(" , "); } JSON.string(buf, name); buf.append(" : "); serializer.serialize(dbo.get(name), buf); } buf.append("}"); }
/** * Map a DBObject for indexing * * @param dbObj */ private Map<String, Object> createObjectMap(DBObject dbObj) { Map<String, Object> mapData = new HashMap<String, Object>(); for (String key : dbObj.keySet()) { Object value = dbObj.get(key); if (value instanceof DBRef) { mapData.put(key, this.convertDbRef((DBRef) value)); } else if (value instanceof BasicDBList) { mapData.put(key, ((BasicBSONList) value).toArray()); } else if (value instanceof BasicDBObject) { mapData.put(key, this.createObjectMap((DBObject) value)); } else { mapData.put(key, value); } } return mapData; }
@Nullable private DBObject currentObject(@Nullable final DBObject newCurrentObject){ if (newCurrentObject != null) { currentObject = newCurrentObject; numSeen++; DBObject projection = findOptions.getProjection(); if (projection != null && !(projection.keySet().isEmpty())) { currentObject.markAsPartialObject(); } } return newCurrentObject; }
public static DBObject applyIncludeFields(DBObject bsonObject, final Set<String> includeFields) { if (includeFields == null) { return bsonObject; } DBObject filteredObject = new BasicDBObject(); for (String field : bsonObject.keySet()) { if (includeFields.contains(field)) { filteredObject.put(field, bsonObject.get(field)); } } for (String field : includeFields) { if (field.contains(".")) { String rootObject = field.substring(0, field.indexOf(".")); // String childObject = field.substring(field.indexOf(".") + 1); Object object = bsonObject.get(rootObject); if (object instanceof DBObject) { DBObject object2 = (DBObject) object; // object2 = applyIncludeFields(object2, new // HashSet<String>(Arrays.asList(childObject))); System.out.println(getChildItems(rootObject, includeFields)); object2 = applyIncludeFields(object2, getChildItems(rootObject, includeFields)); filteredObject.put(rootObject, object2); } } } return filteredObject; }
static String addRemovePrefix(String prefix, String object, boolean add) { if (prefix == null) { throw new IllegalArgumentException("prefix"); } if (object == null) { throw new NullPointerException("object"); } if (object.length() == 0) { return ""; } DBObject bsonObject = (DBObject) JSON.parse(object); BasicBSONObject newObject = new BasicBSONObject(); for (String key : bsonObject.keySet()) { if (add) { newObject.put(prefix + key, bsonObject.get(key)); } else { if (key.startsWith(prefix)) { newObject.put(key.substring(prefix.length()), bsonObject.get(key)); } else { newObject.put(key, bsonObject.get(key)); } } } return newObject.toString(); }
@Override @Deprecated public DBObject getFieldsObject() { DBObject projection = getOptions().getProjection(); if (projection == null || projection.keySet().isEmpty()) { return null; } final MappedClass mc = ds.getMapper().getMappedClass(clazz); Entity entityAnnotation = mc.getEntityAnnotation(); final BasicDBObject fieldsFilter = copy(projection); if (includeFields && entityAnnotation != null && !entityAnnotation.noClassnameStored()) { fieldsFilter.put(ds.getMapper().getOptions().getDiscriminatorField(), 1); } return fieldsFilter; }
@Override public void encode(final BsonWriter writer, final DBObject document, final EncoderContext encoderContext) { writer.writeStartDocument(); beforeFields(writer, encoderContext, document); for (final String key : document.keySet()) { if (skipField(encoderContext, key)) { continue; } writer.writeName(key); writeValue(writer, encoderContext, document.get(key)); } writer.writeEndDocument(); }
/** * Modify an existing document or documents in collection. * * @param query the selection criteria for the update * @param update the modifications to apply * @param options the options to apply to the update operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/modify-documents/ Modify * @since 3.4 */ public WriteResult update(final DBObject query, final DBObject update, final DBCollectionUpdateOptions options) { notNull("query", query); notNull("update", update); notNull("options", options); WriteConcern writeConcern = options.getWriteConcern() != null ? options.getWriteConcern() : getWriteConcern(); com.mongodb.bulk.WriteRequest.Type updateType = !update.keySet().isEmpty() && update.keySet().iterator().next().startsWith("$") ? com.mongodb.bulk.WriteRequest.Type.UPDATE : com.mongodb.bulk.WriteRequest.Type.REPLACE; UpdateRequest updateRequest = new UpdateRequest(wrap(query), wrap(update, options.getEncoder()), updateType) .upsert(options.isUpsert()).multi(options.isMulti()) .collation(options.getCollation()) .arrayFilters(wrapAllowNull(options.getArrayFilters(), options.getEncoder())); return executeWriteOperation(new UpdateOperation(getNamespace(), true, writeConcern, retryWrites, singletonList(updateRequest)).bypassDocumentValidation(options.getBypassDocumentValidation())); }
private void verifyCoverage(final DBObject dbObject) { for (MappedField field : getMorphia().getMapper().getMappedClass(ReferenceType.class).getPersistenceFields()) { String name = field.getNameToStore(); boolean found = dbObject.containsField(name); if (!found) { for (String s : dbObject.keySet()) { found |= s.startsWith(name + "."); } } assertTrue("Not found in dbObject: " + name, found); } }
@Test public void validateNestedInterfaces() { getMorphia().map(WithNestedValidated.class, Nested.class, NestedImpl.class, AnotherNested.class); try { getDs().ensureIndexes(); } catch (MappingException e) { Assert.assertEquals("Could not resolve path 'nested.field.fail' against 'xyz.morphia.mapping" + ".EmbeddedMappingTest$WithNestedValidated'.", e.getMessage()); } final List<DBObject> indexInfo = getDs().getCollection(WithNestedValidated.class).getIndexInfo(); boolean indexFound = false; for (DBObject dbObject : indexInfo) { indexFound |= "nested.field.fail".equals(((DBObject) dbObject.get("key")).keySet().iterator().next()); } Assert.assertFalse("Should not find the nested field index", indexFound); }
@Override public void toDBObject(final Object entity, final MappedField mf, final DBObject dbObject, final Map<Object, DBObject> involvedObjects, final Mapper mapper) { final String name = mf.getNameToStore(); final Object fieldValue = mf.getFieldValue(entity); if (mf.isMap()) { writeMap(mf, dbObject, involvedObjects, name, fieldValue, mapper); } else if (mf.isMultipleValues()) { writeCollection(mf, dbObject, involvedObjects, name, fieldValue, mapper); } else { //run converters if (mapper.getConverters().hasDbObjectConverter(mf) || mapper.getConverters().hasDbObjectConverter(entity.getClass())) { mapper.getConverters().toDBObject(entity, mf, dbObject, mapper.getOptions()); return; } final DBObject dbObj = fieldValue == null ? null : mapper.toDBObject(fieldValue, involvedObjects); if (dbObj != null) { if (!shouldSaveClassName(fieldValue, dbObj, mf)) { dbObj.removeField(mapper.getOptions().getDiscriminatorField()); } if (!dbObj.keySet().isEmpty() || mapper.getOptions().isStoreEmpties()) { dbObject.put(name, dbObj); } } } }
boolean indexFound = false; for (DBObject dbObject : indexInfo) { indexFound |= "nested.field.fail".equals(((DBObject) dbObject.get("key")).keySet().iterator().next());