下面列出了怎么用com.mongodb.QueryOperators的API类实例代码及写法,或者点击链接到github查看源代码。
void removeTilesWithIds(final StackId stackId,
final List<String> tileIds)
throws IllegalArgumentException {
MongoUtil.validateRequiredParameter("stackId", stackId);
MongoUtil.validateRequiredParameter("tileIds", tileIds);
final MongoCollection<Document> tileCollection = getTileCollection(stackId);
final Document tileQuery = new Document("tileId",
new Document(QueryOperators.IN,
tileIds));
final Document tileQueryForLog = new Document("tileId",
new Document(QueryOperators.IN,
Arrays.asList("list of",
tileIds.size() + " tileIds")));
final DeleteResult removeResult = tileCollection.deleteMany(tileQuery);
LOG.debug("removeTilesWithIds: {}.remove({}) deleted {} document(s)",
MongoUtil.fullName(tileCollection), tileQueryForLog.toJson(), removeResult.getDeletedCount());
}
private List<TransformSpec> getTransformSpecs(final MongoCollection<Document> transformCollection,
final Set<String> specIds) {
final int specCount = specIds.size();
final List<TransformSpec> transformSpecList = new ArrayList<>(specCount);
if (specCount > 0) {
final Document transformQuery = new Document();
transformQuery.put("id", new Document(QueryOperators.IN, specIds));
LOG.debug("getTransformSpecs: {}.find({})",
MongoUtil.fullName(transformCollection), transformQuery.toJson());
try (final MongoCursor<Document> cursor = transformCollection.find(transformQuery).iterator()) {
Document document;
TransformSpec transformSpec;
while (cursor.hasNext()) {
document = cursor.next();
transformSpec = TransformSpec.fromJson(document.toJson());
transformSpecList.add(transformSpec);
}
}
}
return transformSpecList;
}
private Document mergeConjunctions(List<Document> conjunctions) {
final Multimap<String, Document> merged = LinkedHashMultimap.create();
for (Document doc : conjunctions) {
Preconditions.checkState(doc.keySet().size() == 1, "Invalid constraint %s", doc);
final String key = doc.keySet().iterator().next();
merged.put(key, doc);
}
final Document result = new Document();
for (Map.Entry<String, Collection<Document>> entry : merged.asMap().entrySet()) {
Preconditions.checkState(!entry.getValue().isEmpty(), "empty constraint: %s", entry);
if (entry.getValue().size() == 1) {
result.putAll(entry.getValue().iterator().next());
} else {
result.putAll(new Document(QueryOperators.AND, entry.getValue()));
}
}
return result;
}
/**
* Equivalent to a $within operand, based on a bounding polygon represented by an array of points
*
* @param points an array of Double[] defining the vertices of the search area
* @return {@code this}
*/
public QueryBuilder withinPolygon(final List<Double[]> points) {
notNull("points", points);
if (points.isEmpty() || points.size() < 3) {
throw new IllegalArgumentException("Polygon insufficient number of vertices defined");
}
addOperand(QueryOperators.WITHIN,
new Document(QueryOperators.POLYGON, convertToListOfLists(points)));
return this;
}
/**
* Equivalent to a $text operand.
*
* @param search the search terms to apply to the text index.
* @param language the language to use.
* @return {@code this}
* @mongodb.server.release 2.6
*/
public QueryBuilder text(final String search, @Nullable final String language) {
if (currentKey != null) {
throw new QueryBuilderException("The text operand may only occur at the top-level of a query. It does"
+ " not apply to a specific element, but rather to a document as a whole.");
}
put(QueryOperators.TEXT);
addOperand(QueryOperators.SEARCH, search);
if (language != null) {
addOperand(QueryOperators.LANGUAGE, language);
}
return this;
}
/**
* Equivalent to an $or operand
*
* @param ors the list of conditions to or together
* @return {@code this}
*/
public QueryBuilder or(final Document... ors) {
List<Object> l = query.getList(QueryOperators.OR, Object.class);
if (l == null) {
l = new ArrayList<>();
query.put(QueryOperators.OR, l);
}
Collections.addAll(l, ors);
return this;
}
/**
* Equivalent to an $and operand
*
* @param ands the list of conditions to and together
* @return {@code this}
*/
public QueryBuilder and(final Document... ands) {
List<Object> l = query.getList(QueryOperators.AND, Object.class);
if (l == null) {
l = new ArrayList<>();
query.put(QueryOperators.AND, l);
}
Collections.addAll(l, ands);
return this;
}
public void cloneStack(final StackId fromStackId,
final StackId toStackId,
final List<Double> zValues,
final Boolean skipTransforms)
throws IllegalArgumentException, IllegalStateException {
MongoUtil.validateRequiredParameter("fromStackId", fromStackId);
MongoUtil.validateRequiredParameter("toStackId", toStackId);
if ((skipTransforms == null) || (! skipTransforms)) {
final MongoCollection<Document> fromTransformCollection = getTransformCollection(fromStackId);
final MongoCollection<Document> toTransformCollection = getTransformCollection(toStackId);
cloneCollection(fromTransformCollection, toTransformCollection, new Document());
}
final Document filterQuery = new Document();
if ((zValues != null) && (zValues.size() > 0)) {
final BasicDBList list = new BasicDBList();
list.addAll(zValues);
final Document zFilter = new Document(QueryOperators.IN, list);
filterQuery.append("z", zFilter);
}
final MongoCollection<Document> fromTileCollection = getTileCollection(fromStackId);
final MongoCollection<Document> toTileCollection = getTileCollection(toStackId);
cloneCollection(fromTileCollection, toTileCollection, filterQuery);
}
private Document getOutsideGroupQuery(final String groupId) {
final List<Document> queryList = new ArrayList<>();
queryList.add(new Document("pGroupId", groupId).append(
"qGroupId", new Document(QueryOperators.NE, groupId)));
queryList.add(new Document("qGroupId", groupId).append(
"pGroupId", new Document(QueryOperators.NE, groupId)));
return new Document(QueryOperators.OR, queryList);
}
private Document getInvolvingObjectQuery(final String groupId,final String id){
final List<Document> queryList = new ArrayList<>();
queryList.add(new Document("pGroupId", groupId).append(
"pId", id));
queryList.add(new Document("qGroupId", groupId).append(
"qId", id));
return new Document(QueryOperators.OR, queryList);
}
private Document getInvolvingObjectAndGroupQuery(final String groupId,final String id, final String qGroupId){
final List<Document> queryList = new ArrayList<>();
queryList.add(new Document("pGroupId", groupId).append(
"pId", id).append("qGroupId",qGroupId));
queryList.add(new Document("qGroupId", groupId).append(
"qId", id).append("pGroupId",qGroupId));
return new Document(QueryOperators.OR, queryList);
}
@Override
public ConstraintBuilder in(String name, boolean negate, Iterable<?> values) {
addContraint(name,
new Document(
negate ? QueryOperators.NIN : QueryOperators.IN,
ImmutableSet.copyOf(unwrapBsonableIterable(values))));
return this;
}
/**
* @return list of section data objects for the specified stackId.
*
* @throws IllegalArgumentException
* if any required parameters are missing or the stack section data has not been built.
*
* @throws ObjectNotFoundException
* if the stack cannot be found.
*/
public List<SectionData> getSectionData(final StackId stackId,
final Double minZ,
final Double maxZ)
throws IllegalArgumentException, ObjectNotFoundException {
MongoUtil.validateRequiredParameter("stackId", stackId);
final List<SectionData> list = new ArrayList<>();
if (! MongoUtil.exists(renderDatabase, stackId.getSectionCollectionName())) {
throwExceptionIfStackIsMissing(stackId);
throw new IllegalArgumentException("section data not aggregated for " + stackId +
", set stack state to COMPLETE to generate the aggregate collection");
}
final MongoCollection<Document> sectionCollection = getSectionCollection(stackId);
final Document query = new Document();
if (minZ != null) {
if (maxZ != null) {
query.append("_id.z", new Document(QueryOperators.GTE, minZ).append(QueryOperators.LTE, maxZ));
} else {
query.append("_id.z", new Document(QueryOperators.GTE, minZ));
}
} else if (maxZ != null) {
query.append("_id.z", new Document(QueryOperators.LTE, maxZ));
}
try (final MongoCursor<Document> cursor = sectionCollection.find(query).iterator()) {
Document document;
Document resultId;
String sectionId;
Number tileCount;
Long tileCountLong = null;
Double z;
Double minX;
Double maxX;
Double minY;
Double maxY;
while (cursor.hasNext()) {
document = cursor.next();
resultId = document.get("_id", Document.class);
sectionId = resultId.get("sectionId", String.class);
z = resultId.get("z", Double.class);
// Need to convert tileCount to long this way because aggregation seems
// to create an Integer if the tileCount is small enough.
// Using a long "just in case" there is a section with more than 2^31-1 tiles.
tileCount = document.get("tileCount", Number.class);
if (tileCount != null) {
tileCountLong = tileCount.longValue();
}
minX = document.getDouble("minX");
maxX = document.getDouble("maxX");
minY = document.getDouble("minY");
maxY = document.getDouble("maxY");
if ((sectionId != null) && (z != null)) {
list.add(new SectionData(sectionId, z, tileCountLong, minX, maxX, minY, maxY));
}
}
}
LOG.debug("getSectionData: returning {} values for {}.find({})",
list.size(), sectionCollection.getNamespace().getFullName());
return list;
}
private Document lte(final double value) {
return new Document(QueryOperators.LTE, value);
}
private Document gte(final double value) {
return new Document(QueryOperators.GTE, value);
}
private Document getGroupQuery(final Double minZ,
final Double maxZ,
final String groupId,
final Double minX,
final Double maxX,
final Double minY,
final Double maxY)
throws IllegalArgumentException {
final Document groupQuery = new Document();
if ((minZ != null) && minZ.equals(maxZ)) {
groupQuery.append("z", minZ);
} else {
if (minZ != null) {
if (maxZ != null) {
groupQuery.append("z", new Document(QueryOperators.GTE, minZ).append(QueryOperators.LTE, maxZ));
} else {
groupQuery.append("z", new Document(QueryOperators.GTE, minZ));
}
} else if (maxZ != null) {
groupQuery.append("z", new Document(QueryOperators.LTE, maxZ));
}
}
if (groupId != null) {
groupQuery.append("groupId", groupId);
}
if (minX != null) {
groupQuery.append("maxX", new Document(QueryOperators.GTE, minX));
}
if (maxX != null) {
groupQuery.append("minX", new Document(QueryOperators.LTE, maxX));
}
if (minY != null) {
groupQuery.append("maxY", new Document(QueryOperators.GTE, minY));
}
if (maxY != null) {
groupQuery.append("minY", new Document(QueryOperators.LTE, maxY));
}
return groupQuery;
}
private Set<String> getMultiConsensusGroupIds(final MatchCollectionId collectionId,
final boolean includeQGroupIds)
throws IllegalArgumentException {
final MongoCollection<Document> matchCollection = getExistingCollection(collectionId);
final List<Document> pipeline = new ArrayList<>();
pipeline.add(new Document("$match",
new Document("consensusSetData",
new Document(QueryOperators.EXISTS, true))));
if (includeQGroupIds) {
// db.<matchCollection>.aggregate(
// [
// { "$match": { "consensusSetData": { "$exists": true } } },
// { "$group": { "_id": { "pGroupId": "$pGroupId", "qGroupId": "$qGroupId" } } }
// ]
// )
pipeline.add(new Document("$group",
new Document("_id",
new Document("pGroupId", "$pGroupId").
append("qGroupId", "$qGroupId"))));
} else {
// db.<matchCollection>.aggregate(
// [
// { "$match": { "consensusSetData": { "$exists": true } } },
// { "$group": { "_id": { "pGroupId": "$pGroupId" } } }
// ]
// )
pipeline.add(new Document("$group",
new Document("_id",
new Document("pGroupId", "$pGroupId"))));
}
if (LOG.isDebugEnabled()) {
LOG.debug("getMultiConsensusGroupIds: running {}.aggregate({})",
MongoUtil.fullName(matchCollection),
MongoUtil.toJson(pipeline));
}
// sort and reduce to distinct set of group ids here instead of in pipeline
final TreeSet<String> groupIdsWithMultiplePairs = new TreeSet<>();
// mongodb java 3.0 driver notes:
// -- need to set cursor batchSize to prevent NPE from cursor creation
final AggregateIterable<Document> iterable = matchCollection.aggregate(pipeline).batchSize(1);
try (final MongoCursor<Document> cursor = iterable.iterator()) {
while (cursor.hasNext()) {
final Document id = cursor.next().get("_id", Document.class);
groupIdsWithMultiplePairs.add(id.getString("pGroupId"));
if (includeQGroupIds) {
groupIdsWithMultiplePairs.add(id.getString("qGroupId"));
}
}
}
return groupIdsWithMultiplePairs;
}
@Override
public ConstraintBuilder equal(String name, boolean negate, @Nullable Object value) {
addContraint(name, negate ? new Document(QueryOperators.NE, unwrapBsonable(value)) : unwrapBsonable(value));
return this;
}
private Object negateConstraint(boolean negate, Object constraint) {
return negate ? new Document(QueryOperators.NOT, constraint) : constraint;
}
@Override
public ConstraintBuilder present(String name, boolean negate) {
addContraint(name, new Document(QueryOperators.EXISTS, !negate));
return this;
}
/**
* Equivalent of the $near operand
*
* @param x x coordinate
* @param y y coordinate
* @param maxDistance max distance
* @return {@code this}
*/
public QueryBuilder near(final double x, final double y, final double maxDistance) {
addOperand(QueryOperators.NEAR,
asList(x, y));
addOperand(QueryOperators.MAX_DISTANCE,
maxDistance);
return this;
}
/**
* Equivalent of the $nearSphere operand
*
* @param longitude coordinate in decimal degrees
* @param latitude coordinate in decimal degrees
* @param maxDistance max spherical distance
* @return {@code this}
*/
public QueryBuilder nearSphere(final double longitude, final double latitude, final double maxDistance) {
addOperand(QueryOperators.NEAR_SPHERE,
asList(longitude, latitude));
addOperand(QueryOperators.MAX_DISTANCE,
maxDistance);
return this;
}
/**
* Equivalent of the $centerSphere operand mostly intended for queries up to a few hundred miles or km.
*
* @param longitude coordinate in decimal degrees
* @param latitude coordinate in decimal degrees
* @param maxDistance max spherical distance
* @return {@code this}
*/
public QueryBuilder withinCenterSphere(final double longitude, final double latitude, final double maxDistance) {
addOperand(QueryOperators.WITHIN,
new Document(QueryOperators.CENTER_SPHERE,
asList(asList(longitude, latitude), maxDistance)));
return this;
}
public Document asDocument() {
final List<Document> result = new ArrayList<>(disjunctions);
result.add(mergeConjunctions(conjunctions));
return result.size() == 1 ? result.get(0) : new Document(QueryOperators.OR, result);
}
/**
* Equivalent to the $gt operator
*
* @param object Value to query
* @return {@code this}
*/
public QueryBuilder greaterThan(final Object object) {
addOperand(QueryOperators.GT, object);
return this;
}
/**
* Equivalent to the $gte operator
*
* @param object Value to query
* @return {@code this}
*/
public QueryBuilder greaterThanEquals(final Object object) {
addOperand(QueryOperators.GTE, object);
return this;
}
/**
* Equivalent to the $lt operand
*
* @param object Value to query
* @return {@code this}
*/
public QueryBuilder lessThan(final Object object) {
addOperand(QueryOperators.LT, object);
return this;
}
/**
* Equivalent to the $lte operand
*
* @param object Value to query
* @return {@code this}
*/
public QueryBuilder lessThanEquals(final Object object) {
addOperand(QueryOperators.LTE, object);
return this;
}
/**
* Equivalent of the $ne operand
*
* @param object Value to query
* @return {@code this}
*/
public QueryBuilder notEquals(final Object object) {
addOperand(QueryOperators.NE, object);
return this;
}
/**
* Equivalent of the $in operand
*
* @param object Value to query
* @return {@code this}
*/
public QueryBuilder in(final Object object) {
addOperand(QueryOperators.IN, object);
return this;
}