Java Code Examples for com.mongodb.client.model.Filters#eq()

The following examples show how to use com.mongodb.client.model.Filters#eq() . 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: MongoChainServiceImpl.java    From nuls-v2 with MIT License 6 votes vote down vote up
@Override
public void rollbackChainList(List<ChainInfo> chainInfoList) {
    if (chainInfoList.isEmpty()) {
        return;
    }
    for (ChainInfo chainInfo : chainInfoList) {
        //缓存的链,数据不清空
        if (CacheManager.getCacheChain(chainInfo.getChainId()) != null) {
            continue;
        }
        if (chainInfo.isNew()) {
            Bson filter = Filters.eq("_id", chainInfo.getChainId());
            mongoDBService.delete(CHAIN_INFO_TABLE, filter);
            CacheManager.removeApiCache(chainInfo.getChainId());
        } else {
            updateChainInfo(chainInfo);
        }
    }
}
 
Example 2
Source File: MongoCompensableLock.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void unlockTransactionInMongoDB(TransactionXid transactionXid, String identifier) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId);
		Bson instIdFilter = Filters.eq("identifier", identifier);

		DeleteResult result = collection.deleteOne(Filters.and(globalFilter, instIdFilter));
		if (result.getDeletedCount() == 0) {
			logger.warn("Error occurred while unlocking transaction(gxid= {}).", instanceId);
		}
	} catch (RuntimeException rex) {
		logger.error("Error occurred while unlocking transaction(gxid= {})!", instanceId, rex);
	}
}
 
Example 3
Source File: MongoCompensableLock.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
private boolean takeOverTransactionInMongoDB(TransactionXid transactionXid, String source, String target) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId);
		Bson instIdFilter = Filters.eq("identifier", source);

		Document document = new Document("$set", new Document("identifier", target));

		UpdateResult result = collection.updateOne(Filters.and(globalFilter, instIdFilter), document);
		return result.getMatchedCount() == 1;
	} catch (RuntimeException rex) {
		logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, rex);
		return false;
	}
}
 
Example 4
Source File: SummaryDao.java    From XBDD with Apache License 2.0 6 votes vote down vote up
public void updateSummary(final Coordinates coordinates) {
	final MongoCollection<Summary> summary = getSummaryCollection();

	final Bson query = Filters.eq(coordinates.getProduct() + "/" + coordinates.getVersionString());
	final Summary summaryObject = summary.find(query, Summary.class).first();

	if (summaryObject != null) { // lookup the summary document
		if (!summaryObject.getBuilds()
				.contains(coordinates.getBuild())) { // only update it if this build hasn't been added to it before.
			summaryObject.getBuilds().add(coordinates.getBuild());
			summary.replaceOne(query, summaryObject);
		}
	} else {
		final Summary newSummary = new Summary();
		newSummary.setId(coordinates.getProduct() + "/" + coordinates.getVersionString());
		newSummary.setBuilds(new ArrayList<>());
		newSummary.getBuilds().add(coordinates.getBuild());

		// Summary's don't care about the build as they have a list of them.
		final CoordinatesDto coordDto = CoordinatesMapper.mapCoordinates(coordinates);
		coordDto.setBuild(null);
		newSummary.setCoordinates(coordDto);

		summary.insertOne(newSummary);
	}
}
 
Example 5
Source File: UsersDao.java    From XBDD with Apache License 2.0 6 votes vote down vote up
public User saveUser(final User user) {
	final MongoCollection<User> users = getUsersColletions();
	final Bson query = Filters.eq("user_id", user.getUserId());

	final User savedUser = users.find(query).first();

	if (savedUser == null) {
		users.insertOne(user);

		return user;
	}

	if (!StringUtils.equals(savedUser.getDisplay(), user.getDisplay())) {
		savedUser.setDisplay(user.getDisplay());
		users.replaceOne(query, savedUser);
	}

	return savedUser;

}
 
Example 6
Source File: MongoCompensableRepository.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void markTransactionRollback(TransactionXid transactionXid) {
	try {
		byte[] global = transactionXid.getGlobalTransactionId();
		String identifier = ByteUtils.byteArrayToString(global);

		String application = CommonUtils.getApplication(this.endpoint);

		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_TRANSACTIONS);

		Document document = new Document();
		document.append("$set", new Document("status", Status.STATUS_MARKED_ROLLBACK));

		Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, identifier);
		Bson statusFilter = Filters.eq("status", Status.STATUS_ACTIVE);

		collection.updateOne(Filters.and(globalFilter, statusFilter), document);
	} catch (RuntimeException error) {
		logger.error("Error occurred while setting the error flag.", error);
	}
}
 
Example 7
Source File: MongodbManager.java    From grain with MIT License 6 votes vote down vote up
/**
 * 修改记录
 * 
 * @param collectionName
 *            表名
 * @param mongoObj
 *            对象
 * @return
 */
public static boolean updateById(String collectionName, MongoObj mongoObj) {
	MongoCollection<Document> collection = getCollection(collectionName);
	try {
		Bson filter = Filters.eq(MongoConfig.MONGO_ID, mongoObj.getDocument().getObjectId(MongoConfig.MONGO_ID));
		mongoObj.setDocument(null);
		Document document = objectToDocument(mongoObj);
		UpdateResult result = collection.updateOne(filter, new Document(MongoConfig.$SET, document));
		if (result.getMatchedCount() == 1) {
			return true;
		} else {
			return false;
		}
	} catch (Exception e) {
		if (log != null) {
			log.error("修改记录失败", e);
		}
		return false;
	}

}
 
Example 8
Source File: MongodbManager.java    From grain with MIT License 6 votes vote down vote up
/**
 * 删除记录
 * 
 * @param collectionName
 *            表名
 * @param mongoObj
 *            记录
 * @return
 */
public static boolean deleteById(String collectionName, MongoObj mongoObj) {
	MongoCollection<Document> collection = getCollection(collectionName);
	try {
		Bson filter = Filters.eq(MongoConfig.MONGO_ID, mongoObj.getDocument().getObjectId(MongoConfig.MONGO_ID));
		DeleteResult result = collection.deleteOne(filter);
		if (result.getDeletedCount() == 1) {
			return true;
		} else {
			return false;
		}
	} catch (Exception e) {
		if (log != null) {
			log.error("删除记录失败", e);
		}
		return false;
	}

}
 
Example 9
Source File: MongoPunishServiceImpl.java    From nuls-v2 with MIT License 6 votes vote down vote up
public PageInfo<PunishLogInfo> getPunishLogList(int chainId, int type, String address, int pageIndex, int pageSize) {
    Bson filter = null;

    if (type == 0 && !StringUtils.isBlank(address)) {
        filter = Filters.eq("address", address);
    } else if (type > 0 && StringUtils.isBlank(address)) {
        filter = Filters.eq("type", type);
    } else if (type > 0 && !StringUtils.isBlank(address)) {
        filter = Filters.and(eq("type", type), eq("address", address));
    }

    long totalCount = mongoDBService.getCount(PUNISH_TABLE + chainId, filter);
    List<Document> documentList = mongoDBService.pageQuery(PUNISH_TABLE + chainId, filter, Sorts.descending("time"), pageIndex, pageSize);
    List<PunishLogInfo> punishLogList = new ArrayList<>();
    for (Document document : documentList) {
        punishLogList.add(DocumentTransferTool.toInfo(document, PunishLogInfo.class));
    }
    PageInfo<PunishLogInfo> pageInfo = new PageInfo<>(pageIndex, pageSize, totalCount, punishLogList);
    return pageInfo;
}
 
Example 10
Source File: ChatActionMongodb.java    From anychat with MIT License 5 votes vote down vote up
public static ChatObj getChatById(String chatId, String chatGroupId) {
	if (StringUtil.stringIsNull(chatId) || StringUtil.stringIsNull(chatGroupId)) {
		return null;
	}
	Bson filter = Filters.eq(ChatObj.CHAT_ID, chatId);
	List<ChatObj> list = MongodbManager.find(chatGroupId, filter, ChatObj.class, 0, 0);
	if (list != null && list.size() != 0) {
		return list.get(0);
	} else {
		return null;
	}
}
 
Example 11
Source File: ChatActionMongodb.java    From anychat with MIT License 5 votes vote down vote up
public static ChatObj getChatById(String chatId, String key1, String key2) {
	if (StringUtil.stringIsNull(chatId) || StringUtil.stringIsNull(key1) || StringUtil.stringIsNull(key2)) {
		return null;
	}
	Bson filter = Filters.eq(ChatObj.CHAT_ID, chatId);
	List<ChatObj> list = MongodbManager.find(getUserToUserCollectionName(key1, key2), filter, ChatObj.class, 0, 0);
	if (list != null && list.size() != 0) {
		return list.get(0);
	} else {
		return null;
	}
}
 
Example 12
Source File: MongoDBClient.java    From redtorch with MIT License 5 votes vote down vote up
/**
 * 通过_id更新
 * 
 * @param dbName
 * @param collectionName
 * @param _id
 * @param document
 * @return
 */
public boolean updateById(String dbName, String collectionName, String _id, Document document) {
	ObjectId objectId = new ObjectId(_id);
	Bson filter = Filters.eq("_id", objectId);

	UpdateResult result = getDatabase(dbName).getCollection(collectionName).updateOne(filter,
			new Document("$set", document));
	long modifiedCount = result.getModifiedCount();

	return modifiedCount > 0 ? true : false;
}
 
Example 13
Source File: Mongo.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void deleteDocument(final BaleenDocument document)
    throws AnalysisEngineProcessException {
  final String documentId = document.getBaleenId();

  final Bson annotationFilter = Filters.eq(AnalysisConstants.BALEEN_DOC_ID, documentId);
  relationCollection.deleteMany(annotationFilter);
  mentionCollection.deleteMany(annotationFilter);
  entityCollection.deleteMany(annotationFilter);

  documentCollection.deleteMany(Filters.eq(AnalysisConstants.BALEEN_ID, documentId));
}
 
Example 14
Source File: MongoCompensableLock.java    From ByteTCC with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean reExitTransactionInMongoDB(TransactionXid transactionXid, String identifier) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		Bson condition = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId);

		Document increases = new Document();
		increases.append("times", -1);

		Document document = new Document();
		document.append("$inc", increases);

		Document target = collection.findOneAndUpdate(condition, document, new FindOneAndUpdateOptions().upsert(true));
		Integer times = target == null ? null : target.getInteger("times");

		return times == null ? true : times <= 0;
	} catch (com.mongodb.MongoWriteException error) {
		logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, error);
		return true;
	} catch (RuntimeException rex) {
		logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, rex);
		return true;
	}
}
 
Example 15
Source File: MongoDao.java    From elepy with Apache License 2.0 4 votes vote down vote up
private Bson idQuery(Serializable id) {
    return Filters.eq("_id", id);
}
 
Example 16
Source File: MongoEntityStorage.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Bson makeVersionFilter(final int version) {
    return Filters.eq(EntityDocumentConverter.VERSION, version);
}
 
Example 17
Source File: MongoDbStore.java    From swellrt with Apache License 2.0 4 votes vote down vote up
private static Bson filterAccountByEmail(String email) {
  return Filters.eq(ACCOUNT_HUMAN_DATA_FIELD + "." + EMAIL_FIELD, email);
}
 
Example 18
Source File: MongoDbStore.java    From swellrt with Apache License 2.0 4 votes vote down vote up
private static Bson filterSignerInfoBy(byte[] signerId) {
  return Filters.eq("_id", signerId);
}
 
Example 19
Source File: MongoDbStore.java    From swellrt with Apache License 2.0 4 votes vote down vote up
private static Bson filterAccountBy(ParticipantId id) {
  return Filters.eq("_id", id.getAddress());
}
 
Example 20
Source File: MongoEventStorage.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Bson makeSubjectFilter(final RyaIRI subject) {
    return Filters.eq(EventDocumentConverter.SUBJECT, subject.getData());
}