/** * Creates a filter that matches all documents that contain the given field. * * @param fieldName the field name * @return the filter * @mongodb.driver.manual reference/operator/query/exists $exists */ public static Bson exists(final String fieldName) { return exists(fieldName, true); }
private void removeConfigPath() { final FindIterable<Document> documentsWithConfigPath = collection.find(exists("configuration_path")); for (Document document : documentsWithConfigPath) { final ObjectId objectId = document.getObjectId("_id"); document.remove("configuration_path"); final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document); if (updateResult.wasAcknowledged()) { LOG.debug("Successfully updated document with ID <{}>", objectId); } else { LOG.error("Failed to update document with ID <{}>", objectId); } } }
@Override public void upgrade() { final FindIterable<Document> documentsWithMissingFields = collection.find(or(not(exists(ContentPack.FIELD_META_ID)), not(exists(ContentPack.FIELD_META_REVISION)))); for (Document document : documentsWithMissingFields) { final ObjectId objectId = document.getObjectId("_id"); LOG.debug("Found document with missing \"id\" or \"rev\" field with ID <{}>", objectId); final String id = document.get("id", objectId.toHexString()); final int rev = document.get("rev", 0); document.put("id", id); document.put("rev", rev); final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document); if (updateResult.wasAcknowledged()) { LOG.debug("Successfully updated document with ID <{}>", objectId); } else { LOG.error("Failed to update document with ID <{}>", objectId); } } } }
@Override public void upgrade() { if (clusterConfigService.get(MigrationCompleted.class) != null) { LOG.debug("Migration already done."); return; } // Do not overwrite an existing default index config boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null; final ImmutableSet.Builder<String> builder = ImmutableSet.builder(); final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE)); for (final Document document : documents) { final ObjectId id = document.getObjectId(FIELD_ID); final String idString = id.toHexString(); final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false); if (!defaultDone && isDefault) { defaultDone = true; clusterConfigService.write(DefaultIndexSetConfig.create(idString)); } final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount(); if (modifiedCount > 0) { LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); builder.add(idString); } else { LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); } } clusterConfigService.write(MigrationCompleted.create(builder.build())); }
/** * Use the given primary to read the oplog. * * @param primary the connection to the replica set's primary node; may not be null */ protected void readOplog(MongoClient primary) { BsonTimestamp oplogStart = source.lastOffsetTimestamp(replicaSet.replicaSetName()); logger.info("Reading oplog for '{}' primary {} starting at {}", replicaSet, primary.getAddress(), oplogStart); // Include none of the cluster-internal operations and only those events since the previous timestamp ... MongoCollection<Document> oplog = primary.getDatabase("local").getCollection("oplog.rs"); Bson filter = Filters.and(Filters.gt("ts", oplogStart), // start just after our last position Filters.exists("fromMigrate", false)); // skip internal movements across shards FindIterable<Document> results = oplog.find(filter) .sort(new Document("$natural", 1)) // force forwards collection scan .oplogReplay(true) // tells Mongo to not rely on indexes .cursorType(CursorType.TailableAwait); // tail and await new data // Read as much of the oplog as we can ... ServerAddress primaryAddress = primary.getAddress(); try (MongoCursor<Document> cursor = results.iterator()) { while (running.get() && cursor.hasNext()) { if (!handleOplogEvent(primaryAddress, cursor.next())) { // Something happened, and we're supposed to stop reading return; } } } }
Filters.exists("fromMigrate", false)); // skip internal movements across shards FindIterable<Document> results = mongo.getDatabase("local") .getCollection("oplog.rs")
@Override public Bson visitSimple(final String fieldName) { return Filters.exists(fieldName); } }
@Override public Bson visitSimple(final String fieldName) { return Filters.exists(fieldName); } }
/** * Creates a filter that matches all documents that contain the given field. * * @param fieldName the field name * @return the filter * @mongodb.driver.manual reference/operator/query/exists $exists */ public static Bson exists(final String fieldName) { return exists(fieldName, true); }
private Bson createEntityTypeQuery(boolean aggregate, ManagedType managedType) { Bson entityTypeQuery = prefixQuery(GLOBAL_ID_KEY, managedType.getName() + "/"); if (!aggregate) { entityTypeQuery = Filters.and(entityTypeQuery, Filters.exists(GLOBAL_ID_ENTITY)); } return entityTypeQuery; }
@Override public BsonFilterExpression visit(NotNullFilter filter, MongoResolutionContext<?> context) { // exists and it is not null return resolveFieldName(filter.getLeftOperand(), context) .map(fn -> Filters.and(Filters.exists(fn), Filters.not(Filters.type(fn, BsonType.NULL)))) .map(bson -> BsonFilterExpression.create(bson)).orElse(null); }
@Override public BsonFilterExpression visit(NullFilter filter, MongoResolutionContext<?> context) { // not exists or it is not null return resolveFieldName(filter.getLeftOperand(), context) .map(fn -> Filters.or(Filters.not(Filters.exists(fn)), Filters.type(fn, BsonType.NULL))) .map(bson -> BsonFilterExpression.create(bson)).orElse(null); }
private Bson createEntityTypeQuery(boolean aggregate, ManagedType managedType) { Bson entityTypeQuery = prefixQuery(GLOBAL_ID_KEY, managedType.getName() + "/"); if (!aggregate) { entityTypeQuery = Filters.and(entityTypeQuery, Filters.exists(GLOBAL_ID_ENTITY)); } return entityTypeQuery; }
@Nonnull @Override public Optional<SingleFeatureBean> containerOf(Id id) { checkNotNull(id, "id"); final String ownerId = idConverter.convert(id); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(ModelDocument.F_CONTAINER)); final Bson projection = include(ModelDocument.F_CONTAINER); final ModelDocument instance = documents.find(filter).projection(projection).first(); return Optional.ofNullable(instance) .map(ModelDocument::getContainer) .map(ContainerDocument::toBean); }
@Nonnull @Override public Optional<ClassBean> metaClassOf(Id id) { checkNotNull(id, "id"); final String ownerId = idConverter.convert(id); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(ModelDocument.F_METACLASS)); final Bson projection = include(ModelDocument.F_METACLASS); final ModelDocument instance = documents.find(filter).projection(projection).first(); return Optional.ofNullable(instance) .map(ModelDocument::getMetaClass) .map(ClassDocument::toBean); }
@Override public void stop(boolean delete) { gridFSBucket.find(Filters.exists(String.format("%s.%s", METADATA_PROPERTY_METADATA, msKey), false)) // .forEach((Block<GridFSFile>) file -> { status.numBinariesGC += 1; status.sizeBinariesGC += file.getLength(); if (delete) { gridFSBucket.delete(file.getId()); } }); startTime = 0; } }
private Bson getQueryFindMedia(String hash, String api, String mediaType) { Bson addQuery = api == null ? not(exists("metadata.api")) : eq("metadata.api", api); return and(eq("metadata.type", mediaType), eq("metadata.hash", hash), addQuery); }
@Override public void removeContainer(Id id) { checkNotNull(id, "id"); final String ownerId = idConverter.convert(id); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(ModelDocument.F_CONTAINER)); final Bson update = unset(ModelDocument.F_CONTAINER); documents.updateOne(filter, update); }
@Override public void removeValue(SingleFeatureBean feature) { checkNotNull(feature, "feature"); final String ownerId = idConverter.convert(feature.owner()); final String featureId = Integer.toString(feature.id()); final String fieldName = concat(ModelDocument.F_SINGLE_FEATURE, featureId); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName)); final Bson update = unset(fieldName); documents.updateOne(filter, update); }
@Override public void removeAllValues(SingleFeatureBean feature) { checkNotNull(feature, "feature"); final String ownerId = idConverter.convert(feature.owner()); final String fieldName = ModelDocument.F_MANY_FEATURE; final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName)); final Bson update = unset(fieldName); documents.updateOne(filter, update); }