Java Code Examples for com.mongodb.BasicDBObjectBuilder#get()

The following examples show how to use com.mongodb.BasicDBObjectBuilder#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersonConverter.java    From journaldev with MIT License 5 votes vote down vote up
public static DBObject toDBObject(Person p) {

		BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
				.append("name", p.getName()).append("country", p.getCountry());
		if (p.getId() != null)
			builder = builder.append("_id", new ObjectId(p.getId()));
		return builder.get();
	}
 
Example 2
Source File: MongoTemplate.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1})
 */
public void ensureIndex(String collName, String name, String fields, boolean unique, boolean dropDupsOnCreate) {

    BasicDBObject dbFields = parseFieldsString(fields);

    final 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);
        }
    }

    final DBCollection dbColl = getCollection(getCollName(collName));

    final BasicDBObject opts = (BasicDBObject) keyOpts.get();
    if (opts.isEmpty()) {
        LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + dbFields);
        dbColl.createIndex(dbFields);
    } else {
        LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + fields + " and opts:" + opts);
        dbColl.createIndex(dbFields, opts);
    }
}
 
Example 3
Source File: MongoTemplate.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
private BasicDBObject parseFieldsString(final String fields) {
    BasicDBObjectBuilder ret = BasicDBObjectBuilder.start();
    final String[] parts = fields.split(",");
    for (String s : parts) {
        s = s.trim();
        int dir = 1;
        if (s.startsWith("-")) {
            dir = -1;
            s = s.substring(1).trim();
        }
        ret = ret.add(s, dir);
    }
    return (BasicDBObject) ret.get();
}
 
Example 4
Source File: MongoFieldsContext.java    From sissi with Apache License 2.0 5 votes vote down vote up
/**
 * @param field
 * @param builder
 * @param build 是否构建并返回
 * @return
 */
private DBObject entity(Field<?> field, BasicDBObjectBuilder builder, boolean build) {
	String name = null;
	if (field.hasChild()) {
		this.embed(field, builder);
	} else if ((name = this.mapping.mapping(field)) != null) {
		// 如果不存在Field子节点则保存值并终止
		builder.add(name, field.getValue());
	}
	return build ? builder.get() : null;
}
 
Example 5
Source File: MongoAddressing.java    From sissi with Apache License 2.0 5 votes vote down vote up
/**
 * For FindXxx</p>{"jid",Xxx,"resource":Xxx(如果使用资源)}
 * 
 * @param jid
 * @param usingResource
 * @return
 */
private DBObject buildQueryWithSmartResource(JID jid, boolean usingResource) {
	// JID,Resource
	BasicDBObjectBuilder query = BasicDBObjectBuilder.start(Dictionary.FIELD_JID, jid.asStringWithBare());
	if (usingResource && !jid.isBare()) {
		query.add(Dictionary.FIELD_RESOURCE, jid.resource());
	}
	return query.get();
}
 
Example 6
Source File: BridgeExchangerContext.java    From sissi with Apache License 2.0 5 votes vote down vote up
/**
 * {"host":host,"date":Xxx}
 * 
 * @param host
 * @param date
 * @return
 */
private DBObject build(String host, boolean date) {
	BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(Dictionary.FIELD_HOST, host);
	if (date) {
		builder.add(this.date, System.currentTimeMillis());
	}
	return builder.get();
}
 
Example 7
Source File: MongoFieldsContext.java    From sissi with Apache License 2.0 4 votes vote down vote up
protected DBObject entities(Fields fields, BasicDBObjectBuilder builder) {
	for (Field<?> field : fields) {
		this.entity(field, builder, false);
	}
	return builder.get();
}