List of usage examples for com.mongodb DBObject put
Object put(String key, Object v);
From source file:com.ebay.cloud.cms.dal.search.impl.criteria.JoinSearchCriteria.java
License:Apache License
@Override public DBObject translate(final List<DBObject> subQueryList) { DBObject query = new BasicDBObject(); if (idList.size() == 1) { query.put(searchField.getFullDbName(), idList.iterator().next()); } else {// w ww. ja v a 2 s.c o m List<Object> oidList = new ArrayList<Object>(idList.size()); for (String id : idList) { oidList.add(id); } DBObject inQry = new BasicDBObject("$in", oidList); query.put(searchField.getFullDbName(), inQry); } return query; }
From source file:com.ebay.cloud.cms.dal.search.impl.criteria.LogicalSearchCriteria.java
License:Apache License
@Override public DBObject translate(final List<DBObject> subQueryList) { DBObject query = new BasicDBObject(); if (operator == LogicOperatorEnum.AND) { query.put("$and", subQueryList); } else {//from w w w. j a v a 2 s. co m query.put("$or", subQueryList); } return query; }
From source file:com.ebay.cloud.cms.dal.search.impl.criteria.RegExpCriteriaHandler.java
License:Apache License
@Override public DBObject translate(final FieldSearchCriteria criteria) { DBObject query = new BasicDBObject(); String fullDbName = criteria.getFullDbName(); Pattern regex = getRegPattern(criteria); FieldOperatorEnum op = criteria.getOperator(); switch (op) { case REGEX:/*from w w w. j a v a 2 s . co m*/ BasicDBObject dbo = new BasicDBObject(); dbo.put("$regex", regex); query.put(fullDbName, dbo); break; case NREGEX: query.put(fullDbName, new BasicDBObject("$not", regex)); break; default: throw new IllegalArgumentException("Unsupport comparision operator: " + op); } return query; }
From source file:com.ebay.cloud.cms.dal.search.impl.query.EmbedSearchQuery.java
License:Apache License
public MongoQuery buildMongoQuery(PersistenceContext context) { MongoQuery emptyQuery = new MongoQuery(); // pre-order : generate mongo query of current search query MongoQuery mongoQuery = super.buildMongoQuery(context); // traversal child embed query MongoQuery childMongoQuery = null;//w w w.j a va 2 s. com if (this.childQuery != null) { childMongoQuery = childQuery.buildMongoQuery(context); } if (leftJoin) { // for outer_join, we need bypass the child query return emptyQuery; } else if (this.childQuery == null) { return mongoQuery; } hasGroup = childMongoQuery.group != null || mongoQuery.group != null; // trim operator before further operation mongoQuery.match = trimOperator(mongoQuery.match, DOLLAR_MATCH); mongoQuery.project = trimOperator(mongoQuery.project, DOLLAR_PROJECT); childMongoQuery.match = trimOperator(childMongoQuery.match, DOLLAR_MATCH); childMongoQuery.project = trimOperator(childMongoQuery.project, DOLLAR_PROJECT); // projection of child query // rule 1 : group query decide the project if group exists // rule 2 : merge projection if no group if (childMongoQuery.group != null) { // group query decides the projection mongoQuery.project = childMongoQuery.project; } else if (!hasGroup && childMongoQuery.project != null) { // merge projections mongoQuery.project.putAll(childMongoQuery.project); } // aggregation of child query if (childMongoQuery.group != null || childMongoQuery.groupMatch != null) { mongoQuery.group = childMongoQuery.group; mongoQuery.preGroupUnwinds = childMongoQuery.preGroupUnwinds; mongoQuery.groupMatch = childMongoQuery.groupMatch; } // selection of child query if (childMongoQuery.match != null) { if (mongoQuery.match == null) { mongoQuery.match = childMongoQuery.match; } else { List<DBObject> queryMatchList = new ArrayList<DBObject>(2); queryMatchList.add(mongoQuery.match); queryMatchList.add(childMongoQuery.match); LogicalSearchCriteria andCriteria = new LogicalSearchCriteria(LogicOperatorEnum.AND); mongoQuery.match = andCriteria.translate(queryMatchList); } } if (hasGroup) { DBObject match = new BasicDBObject(); match.put(DOLLAR_MATCH, mongoQuery.match); mongoQuery.match = match; DBObject project = new BasicDBObject(); project.put(DOLLAR_PROJECT, mongoQuery.project); mongoQuery.project = project; } return mongoQuery; }
From source file:com.ebay.cloud.cms.dal.search.impl.query.SearchQuery.java
License:Apache License
private void translateGroup() { if (this.group == null) { return;/* w w w .j a v a 2 s .c o m*/ } // translate group this.mongoQuery.preGroupUnwinds = this.group.buildUnwindQuery(); this.mongoQuery.group = this.group.buildGroupQuery(); // translate group criteria if (groupCriteria != null) { DBObject groupCriteriaObject = generateMongoQuery(groupCriteria); this.mongoQuery.groupMatch = new BasicDBObject(); this.mongoQuery.groupMatch.put("$match", groupCriteriaObject); } // wrap projection DBObject projectWrapper = new BasicDBObject(); projectWrapper.put("$project", this.mongoQuery.project); this.mongoQuery.project = projectWrapper; // wrap search criteria if (searchCriteria != null) { DBObject searchCriteriaObject = this.mongoQuery.match; this.mongoQuery.match = new BasicDBObject(); this.mongoQuery.match.put("$match", searchCriteriaObject); } }
From source file:com.ebay.cloud.cms.dal.search.impl.SearchServiceImpl.java
License:Apache License
private DBObject buildAggregationObjects(MongoQuery mongoQuery, SearchOption option, List<DBObject> aggrObjects) { DBObject firstAggr;//from w w w . j av a2 s . co m List<DBObject> allAggrObjects = new ArrayList<DBObject>(); // set pipeline db objects addToList(mongoQuery.match, allAggrObjects); addToList(mongoQuery.preGroupUnwinds, allAggrObjects); addToList(mongoQuery.group, allAggrObjects); addToList(mongoQuery.groupMatch, allAggrObjects); addToList(mongoQuery.project, allAggrObjects); // set search options if (option.hasSort()) { DBObject sortObject = new BasicDBObject(); sortObject.put("$sort", option.getSort()); allAggrObjects.add(sortObject); } if (option.hasSkip()) { DBObject skipObject = new BasicDBObject(); skipObject.put("$skip", option.getSkip()); allAggrObjects.add(skipObject); } if (option.hasLimit()) { DBObject limitObject = new BasicDBObject(); limitObject.put("$limit", option.getLimit()); allAggrObjects.add(limitObject); } // set return object firstAggr = allAggrObjects.get(0); aggrObjects.addAll(allAggrObjects.subList(1, allAggrObjects.size())); return firstAggr; }
From source file:com.ebay.cloud.cms.dal.search.SearchGroup.java
License:Apache License
public final List<DBObject> buildUnwindQuery() { List<DBObject> results = null; for (GroupField groupField : grpFields.values()) { if (CardinalityEnum.Many.equals(groupField.getSearchFiled().getRootField().getCardinality())) { // build unwind object if find any group field cardinality=many if (results == null) { results = new ArrayList<DBObject>(); }/*from w ww. j a va 2 s .c om*/ DBObject unwind = new BasicDBObject(); unwind.put("$unwind", '$' + groupField.getSearchFiled().getFullDbName()); results.add(unwind); } } return results; }
From source file:com.ebay.cloud.cms.dal.search.SearchGroup.java
License:Apache License
private void translateAggregation(BasicDBObject group) { for (AggregationField aggField : aggFields.values()) { DBObject grpAttribute = new BasicDBObject(); if (aggField.func == AggFuncEnum.COUNT) { grpAttribute.put("$" + AggFuncEnum.SUM.getName(), 1); } else {//www . j a v a2 s. co m grpAttribute.put("$" + aggField.func.getName(), "$" + aggField.getSearchField().getFullDbName()); } group.append(aggField.getFieldName(), grpAttribute); } }
From source file:com.ebay.cloud.cms.dal.search.SearchOption.java
License:Apache License
public DBObject getSort() { DBObject sortObj = new BasicDBObject(); for (int i = 0; i < sortFields.size(); i++) { Integer order = sortOrders.size() > i ? sortOrders.get(i) : ASC_ORDER; ISearchField field = sortFields.get(i); sortObj.put(field.getFullDbName(), order); }// w ww.j a v a 2 s .com return sortObj; }
From source file:com.ebay.cloud.cms.dalapi.persistence.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected DBObject buildGetQuery(BitSet arrayBits, String embedId, MetaClass rootMetaClass) { DBObject queryObject = new BasicDBObject(); queryObject.put(InternalFieldEnum.BRANCH.getDbName(), branchId); queryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString()); String embedPath = helper.getEmbedPath(embedId, rootMetaClass); if (StringUtils.isNullOrEmpty(embedPath)) { // generate field for root queryObject.put(InternalFieldEnum.ID.getDbName(), embedId); queryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString()); } else if (arrayBits.isEmpty()) { // generate dot notation field for single String idFieldName = embedPath + EntityIDHelper.DOT + InternalFieldEnum.ID.getDbName(); String statusFieldName = embedPath + EntityIDHelper.DOT + InternalFieldEnum.STATUS.getDbName(); queryObject.put(idFieldName, embedId); queryObject.put(statusFieldName, StatusEnum.ACTIVE.toString()); } else {/*from w w w. j a v a2s .com*/ // generate $elemMatch for array DBObject idQueryObject = new BasicDBObject(); idQueryObject.put(InternalFieldEnum.ID.getDbName(), embedId); idQueryObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString()); BasicDBObject elemMatchObject = new BasicDBObject(); elemMatchObject.put("$elemMatch", idQueryObject); queryObject.put(embedPath, elemMatchObject); } return queryObject; }