Refine search
DBObject document1 = new BasicDBObject(); document1.put("name", "Kiran"); document1.put("age", 20); DBObject document2 = new BasicDBObject(); document2.put("name", "John"); List<DBObject> documents = new ArrayList<>(); documents.add(document1); documents.add(document2); collection.insert(documents);
@Override public AggregationPipeline group(final String id, final Group... groupings) { DBObject group = new BasicDBObject(); group.put("_id", id != null ? "$" + id : null); for (Group grouping : groupings) { group.putAll(toDBObject(grouping)); } stages.add(new BasicDBObject("$group", group)); return this; }
/** * Equivalent to an $or operand * * @param ors the list of conditions to or together * @return {@code this} */ @SuppressWarnings("unchecked") public QueryBuilder or(final DBObject... ors) { List l = (List) _query.get(QueryOperators.OR); if (l == null) { l = new ArrayList(); _query.put(QueryOperators.OR, l); } Collections.addAll(l, ors); return this; }
public BasicDBList getList(String key) { DBCollection coll = getCollection(); DBObject query = new BasicDBObject(); query.put("key", key); DBObject result = coll.findOne(query); return (BasicDBList) result.get("value"); }
@Override public void addTo(final DBObject obj) { BasicDBObjectBuilder query; FilterOperator operator = getOperator(); GeometryQueryConverter geometryQueryConverter = new GeometryQueryConverter(getQuery().getDatastore().getMapper()); final DBObject geometryAsDBObject = (DBObject) geometryQueryConverter.encode(geometry, null); switch (operator) { case NEAR: case NEAR_SPHERE: if (options != null) { geometryAsDBObject.putAll(options); } query = BasicDBObjectBuilder.start(NEAR.val(), geometryAsDBObject); break; case GEO_WITHIN: case INTERSECTS: query = BasicDBObjectBuilder.start(operator.val(), geometryAsDBObject); if (crs != null) { ((DBObject) geometryAsDBObject.get("$geometry")).put("crs", new NamedCoordinateReferenceSystemConverter().encode(crs)); } break; default: throw new UnsupportedOperationException(String.format("Operator %s not supported for geo-query", operator.val())); } obj.put(getField(), query.get()); } }
public boolean getBoolean(String key) { DBCollection coll = getCollection(); DBObject query = new BasicDBObject(); query.put("key", key); DBObject result = coll.findOne(query); if (result == null) { return false; } if (result.get("value").equals(true)) { return true; } return false; }
@Override public DBObject convert(DataPointId id) { DBObject object = new BasicDBObject(FIELDS); object.put("date", id.getDate()); object.put("account", id.getAccount()); return object; } }
/** * Equivalent to an $and operand * * @param ands the list of conditions to and together * @return {@code this} */ @SuppressWarnings("unchecked") public QueryBuilder and(final DBObject... ands) { List l = (List) _query.get(QueryOperators.AND); if (l == null) { l = new ArrayList(); _query.put(QueryOperators.AND, l); } Collections.addAll(l, ands); return this; }
@Override @Nullable public MongoDbSession load(String sessionId) { DBObject query = new BasicDBObject(); query.put(MongoDbSession.FIELD_SESSION_ID, sessionId); DBObject result = findOne(MongoDbSession.class, query); if (result == null) { return null; } final Object objectId = result.get("_id"); return new MongoDbSession((ObjectId) objectId, result.toMap()); }
@Override public AggregationPipeline bucketAuto(final String field, final int bucketCount, final BucketAutoOptions options) { if (bucketCount < 1) { throw new RuntimeException("bucket count should be more than 0"); } DBObject dbObject = options.toDBObject(); dbObject.put("groupBy", "$" + field); dbObject.put("buckets", bucketCount); stages.add(new BasicDBObject("$bucketAuto", dbObject)); return this; }
/** * Adds a new key to the query if not present yet. Sets this key as the current key. * * @param key MongoDB document key * @return {@code this} */ public QueryBuilder put(final String key) { _currentKey = key; if (_query.get(key) == null) { _query.put(_currentKey, new NullObject()); } return this; }
@Override @SuppressWarnings("unchecked") public AccessToken load(String token) { DBObject query = new BasicDBObject(); query.put(AccessTokenImpl.TOKEN, token); final List<DBObject> objects = query(AccessTokenImpl.class, query); if (objects.isEmpty()) { return null; } if (objects.size() > 1) { LOG.error("Multiple access tokens found, this is a serious bug."); throw new IllegalStateException("Access tokens collection has no unique index!"); } final DBObject tokenObject = objects.get(0); final Object id = tokenObject.get("_id"); return new AccessTokenImpl((ObjectId) id, tokenObject.toMap()); }
/** * Sets the sort value for the update * * @param field the field to sort by * @param direction the direction of the sort * @return this */ public PushOptions sort(final String field, final int direction) { if (sort != null) { throw new IllegalStateException("sortDocument can not be set if sort already is"); } if (sortDocument == null) { sortDocument = new BasicDBObject(); } sortDocument.put(field, direction); return this; }
/** * Add an insert request to the bulk operation * * @param document the document to insert */ public void insert(final DBObject document) { isTrue("already executed", !closed); if (document.get(ID_FIELD_NAME) == null) { document.put(ID_FIELD_NAME, new ObjectId()); } addRequest(new InsertRequest(document, collection.getObjectCodec())); }
@Override @SuppressWarnings("unchecked") public List<AccessToken> loadAll(String username) { DBObject query = new BasicDBObject(); query.put(AccessTokenImpl.USERNAME, username); final List<DBObject> objects = query(AccessTokenImpl.class, query); List<AccessToken> tokens = Lists.newArrayList(); for (DBObject tokenObject : objects) { final Object id = tokenObject.get("_id"); final AccessToken accessToken = new AccessTokenImpl((ObjectId) id, tokenObject.toMap()); tokens.add(accessToken); } return tokens; }
@Override public AggregationPipeline bucket(final String field, final List<?> boundaries, final BucketOptions options) { if (boundaries == null || boundaries.size() < 2) { throw new RuntimeException("Boundaries list should be present and has at least 2 elements"); } DBObject dbObject = options.toDBObject(); dbObject.put("groupBy", "$" + field); dbObject.put("boundaries", boundaries); stages.add(new BasicDBObject("$bucket", dbObject)); return this; }
private <T> DBObject toDbObject(final T ent, final Map<Object, DBObject> involvedObjects) { final MappedClass mc = mapper.getMappedClass(ent); if (mc.getAnnotation(NotSaved.class) != null) { throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName())); } DBObject dbObject = entityToDBObj(ent, involvedObjects); List<MappedField> versionFields = mc.getFieldsAnnotatedWith(Version.class); for (MappedField mappedField : versionFields) { String name = mappedField.getNameToStore(); if (dbObject.get(name) == null) { dbObject.put(name, 1); mappedField.setFieldValue(ent, 1L); } } return dbObject; }
@Override public List<SystemMessage> all(int page) { List<SystemMessage> messages = Lists.newArrayList(); DBObject sort = new BasicDBObject(); sort.put("timestamp", -1); List<DBObject> results = query(SystemMessageImpl.class, new BasicDBObject(), sort, PER_PAGE, PER_PAGE * page); for (DBObject o : results) { messages.add(new SystemMessageImpl(new ObjectId(o.get("_id").toString()), o.toMap())); } return messages; }
@Override public void write(Object source, Bson target) { Document document = (Document) target; String strPerson = (String) source; String[] parsedStrPerson = StringUtils.tokenizeToStringArray(strPerson, ","); document.put("fname", parsedStrPerson[0]); document.put("lname", parsedStrPerson[1]); DBObject innerObject = new BasicDBObject(); innerObject.put("city", parsedStrPerson[2]); innerObject.put("street", parsedStrPerson[3]); innerObject.put("zip", parsedStrPerson[4]); innerObject.put("state", parsedStrPerson[5]); document.put("address", innerObject); }
/** * <p>Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added.</p> * * <p>If the value of the continueOnError property of the given {@code InsertOptions} is true, * that value will override the value of the continueOnError property of the given {@code WriteConcern}. Otherwise, * the value of the continueOnError property of the given {@code WriteConcern} will take effect. </p> * * @param documents a list of {@code DBObject}'s to be inserted * @param insertOptions the options to use for the insert * @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 insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents, final InsertOptions insertOptions) { WriteConcern writeConcern = insertOptions.getWriteConcern() != null ? insertOptions.getWriteConcern() : getWriteConcern(); Encoder<DBObject> encoder = toEncoder(insertOptions.getDbEncoder()); List<InsertRequest> insertRequestList = new ArrayList<InsertRequest>(documents.size()); for (DBObject cur : documents) { if (cur.get(ID_FIELD_NAME) == null) { cur.put(ID_FIELD_NAME, new ObjectId()); } insertRequestList.add(new InsertRequest(new BsonDocumentWrapper<DBObject>(cur, encoder))); } return insert(insertRequestList, writeConcern, insertOptions.isContinueOnError(), insertOptions.getBypassDocumentValidation()); }