Package com.mongodb

Examples of com.mongodb.BasicDBObjectBuilder


    public synchronized boolean setHead(RecordId base, RecordId head) {
        if (!base.equals(getHead())) {
            return false;
        }

        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
        builder.add("_id", name);
        if (state.containsField("parent")) {
            builder.add("parent", state.get("parent"));
        }
        if (state.containsField("base")) {
            builder.add("base", state.get("base"));
        }
        builder.add("head", head.toString());
        DBObject nextState = builder.get();

        WriteResult result =
                journals.update(state, nextState, false, false, concern);
        if (result.getN() == 1) {
            state = nextState;
View Full Code Here


                head = newHead;
            }

            base = head;

            BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
            builder.add("_id", name);
            builder.add("parent", state.get("parent"));
            builder.add("base", base.toString());
            builder.add("head", head.toString());
            // TODO: concurrent updates?
            journals.update(state, builder.get(), false, false, concern);
        }
    }
View Full Code Here

     * {name: "Jim", someBinData: new BinData(2,"1234")}<p>
     */
    public static List<DBObject> getDocuments() {
        final List<DBObject> examples = new ArrayList<>();
        examples.add(
            new BasicDBObjectBuilder()
                .add("name", "Tom")
                .add("bio", "A nice guy.")
                .add("pets", Arrays.asList("monkey", "fish"))
                .add("someWeirdLegacyKey", "I like Ike!")
            .get()
        );
        examples.add(
            new BasicDBObjectBuilder()
                .add("name", "Dick")
                .add("bio", "I swordfight.")
                .add("birthday", LocalDate.of(1974, 3, 14).toString())
            .get()
        );
        examples.add(
            new BasicDBObjectBuilder()
                .add("name", "Harry")
                .add("pets", "egret")
                .add("birthday", LocalDate.of(1984, 3, 14).toString())
            .get()
        );
        examples.add(
            new BasicDBObjectBuilder()
                .add("name", "Geneviève")
                .add("bio", "Ça va?")
            .get()
        );
        examples.add(
            new BasicDBObjectBuilder()
                .add("name", "Jim")
                .add("someBinData", new Binary((byte) 0x02, new byte[]{1,2,3,4}))
            .get()
        );

View Full Code Here

      // don't let's be silly
      return;
    }
   
    // shard the new output collection
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.add("enableSharding", database);
    admindb.command(builder.get());
   
    builder = new BasicDBObjectBuilder();
    builder.add("shardCollection", database + "." + collection);
   
    // just shard on _id - but user gets to decide what the _id is
    builder.add("key", new BasicDBObject("_id", 1));  
    admindb.command(builder.get());
   
    // pre-split to get parallel writes
    // this http://www.mongodb.org/display/DOCS/Splitting+Chunks says
    // balancer moving chunks should take 5 minutes ... too long
    // wonder if moveChunk command is faster
View Full Code Here

      for(IntWritable value : values) {
        sum += value.get();
      }
 
      // create a mongo document
      BasicDBObjectBuilder b = new BasicDBObjectBuilder();
      b.add("count", sum);
     
      // key outputted becomes the mongo document's _id
      context.write(word, b.get());
    }
View Full Code Here

      // don't let's be silly - nice sharded cluster, no shard
      return;
    }
   
    // shard the new output collection
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.add("enableSharding", database);
    admindb.command(builder.get());
   
    builder = new BasicDBObjectBuilder();
    builder.add("shardCollection", database + "." + collection);
   
    // just shard on _id - but user gets to decide what the _id is
    builder.add("key", new BasicDBObject("_id", 1));  
    admindb.command(builder.get());
   
    // pre-split to get parallel writes
    // this http://www.mongodb.org/display/DOCS/Splitting+Chunks says
    // balancer moving chunks should take 5 minutes ... too long
    // wonder if moveChunk command is faster
View Full Code Here

    /**
     * parses the string and validates each part
     */
    @SuppressWarnings("rawtypes")
    public static BasicDBObject parseFieldsString(String str, Class clazz, Mapper mapr, boolean validate) {
        BasicDBObjectBuilder ret = BasicDBObjectBuilder.start();
        String[] parts = str.split(",");
        for (String s : parts) {
            s = s.trim();
            int dir = 1;

            if (s.startsWith("-")) {
                dir = -1;
                s = s.substring(1).trim();
            }

            if (validate) {
                StringBuffer sb = new StringBuffer(s);
                DefaultMapper.validate(clazz, mapr, sb, FilterOperator.IN, "", true, false);
                s = sb.toString();
            }
            ret = ret.add(s, dir);
        }
        return (BasicDBObject) ret.get();
    }
View Full Code Here

        this.opts = opts;
    }

    @Override
    public void addTo(DBObject obj) {
        BasicDBObjectBuilder query = null;
        switch (operator) {
            case NEAR:
                query = BasicDBObjectBuilder.start(FilterOperator.NEAR.val(), value);
                break;
            case NEAR_SPHERE:
                query = BasicDBObjectBuilder.start(FilterOperator.NEAR_SPHERE.val(), value);
                break;
            case WITHIN_BOX:
                query = BasicDBObjectBuilder.start().push(FilterOperator.WITHIN.val()).add(operator.val(), value);
                break;
            case WITHIN_CIRCLE:
                query = BasicDBObjectBuilder.start().push(FilterOperator.WITHIN.val()).add(operator.val(), value);
                break;
            case WITHIN_CIRCLE_SPHERE:
                query = BasicDBObjectBuilder.start().push(FilterOperator.WITHIN.val()).add(operator.val(), value);
                break;
            default:
                throw new UnsupportedOperationException(operator + " not supported for geo-query");
        }

        //add options...
        if (opts != null)
            for (Map.Entry<String, Object> e : opts.entrySet())
                query.append(e.getKey(), e.getValue());

        obj.put(field, query.get());
    }
View Full Code Here

     * @param dropDupsOnCreate
     * @param background
     */
    @SuppressWarnings({"rawtypes"})
    public void ensureIndex(Class clazz, String name, IndexFieldDef[] defs, boolean unique, boolean dropDupsOnCreate, boolean background) {
        BasicDBObjectBuilder keys = BasicDBObjectBuilder.start();

        for (IndexFieldDef def : defs) {
            String fieldName = def.getField();
            IndexDirection dir = def.getDirection();
            keys.add(fieldName, dir.toIndexValue());
        }

        ensureIndex(clazz, name, (BasicDBObject) keys.get(), unique, dropDupsOnCreate, background, false, -1);
    }
View Full Code Here

     * @param dropDupsOnCreate
     * @param background
     * @param sparse
     */
    protected <T> void ensureIndex(Class<T> clazz, String name, BasicDBObject fields, boolean unique, boolean dropDupsOnCreate, boolean background, boolean sparse, int expireAfterSeconds) {
        BasicDBObjectBuilder keyOpts = new BasicDBObjectBuilder();
        if (name != null && name.length() > 0) {
            keyOpts.add("name", name);
        }
        if (unique) {
            keyOpts.add("unique", true);
            if (dropDupsOnCreate)
                keyOpts.add("dropDups", true);
        }

        if (background)
            keyOpts.add("background", true);
        if (sparse)
            keyOpts.add("sparse", true);
        if (expireAfterSeconds > -1) {
            keyOpts.add("expireAfterSeconds", expireAfterSeconds);
        }

        DBCollection dbColl = getCollection(clazz);

        BasicDBObject opts = (BasicDBObject) keyOpts.get();
        if (opts.isEmpty()) {
            log.debug("Ensuring index for " + dbColl.getName() + " with keys:" + fields);
            dbColl.ensureIndex(fields);
        } else {
            log.debug("Ensuring index for " + dbColl.getName() + " with keys:" + fields + " and opts:" + opts);
View Full Code Here

TOP

Related Classes of com.mongodb.BasicDBObjectBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.