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

The following examples show how to use com.mongodb.BasicDBObjectBuilder#add() . 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: MongoAffiliationBuilder.java    From sissi with Apache License 2.0 6 votes vote down vote up
@Override
public boolean update(JID jid, String affiliation) {
	try {
		// {"affiliations.$.affiliation":Xxx}
		BasicDBObjectBuilder entity = BasicDBObjectBuilder.start().add(Dictionary.FIELD_AFFILIATIONS + ".$." + Dictionary.FIELD_AFFILIATION, affiliation);
		if (ItemAffiliation.OWNER.equals(affiliation)) {
			// {"creator":jid.bare}
			// 如果为Owner则同时更新创建者
			entity.add(Dictionary.FIELD_CREATOR, jid.asStringWithBare());
		}
		// {"jid":group.bare,"affiliations.jid":jid.bare"},{"$set":...entity...}
		return MongoUtils.success(MongoAffiliationBuilder.this.config.collection().update(BasicDBObjectBuilder.start().add(Dictionary.FIELD_JID, this.group.asStringWithBare()).add(Dictionary.FIELD_AFFILIATIONS + "." + Dictionary.FIELD_JID, jid.asStringWithBare()).get(), BasicDBObjectBuilder.start().add("$set", entity.get()).get(), true, false, WriteConcern.SAFE));
	} catch (MongoException e) {
		// {"jid":group.bare},{"$addToSet":{"affiliations":{"jid":Xxx,"affiliation":Xxx}}}
		return MongoUtils.success(MongoAffiliationBuilder.this.config.collection().update(BasicDBObjectBuilder.start().add(Dictionary.FIELD_JID, this.group.asStringWithBare()).get(), BasicDBObjectBuilder.start("$addToSet", BasicDBObjectBuilder.start(Dictionary.FIELD_AFFILIATIONS, BasicDBObjectBuilder.start().add(Dictionary.FIELD_JID, jid.asStringWithBare()).add(Dictionary.FIELD_AFFILIATION, affiliation).get()).get()).get(), true, false, WriteConcern.SAFE));
	}
}
 
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: MongoRoomBuilder.java    From sissi with Apache License 2.0 5 votes vote down vote up
public Room push(Fields fields) {
	BasicDBObjectBuilder update = BasicDBObjectBuilder.start();
	for (Field<?> field : fields) {
		RoomConfigParser parser = MongoRoomBuilder.this.parsers.get(field.getName());
		if (parser != null) {
			update.add(Dictionary.FIELD_CONFIGS + "." + parser.field(), parser.parse(field));
		}
	}
	if (!update.isEmpty()) {
		MongoRoomBuilder.this.config.collection().update(this.build(), BasicDBObjectBuilder.start("$set", update.get()).get());
	}
	return this;
}
 
Example 5
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 6
Source File: MongoFieldsContext.java    From sissi with Apache License 2.0 5 votes vote down vote up
private void embed(Field<?> field, BasicDBObjectBuilder builder) {
	Fields fields = field.getChildren();
	String name = null;
	if (fields.isEmbed()) {
		this.entities(fields, builder);
	} else if ((name = this.mapping.mapping(field)) != null) {
		// 非嵌入式则创建新Key保存Field子节点,Key = Field.name
		builder.add(name, this.entities(fields, BasicDBObjectBuilder.start()));
	}
}
 
Example 7
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 8
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();
}