下面列出了怎么用com.mongodb.BasicDBObject的API类实例代码及写法,或者点击链接到github查看源代码。
private Map<String, ?> getBsonKeyValueMap(Object bson) {
if (bson instanceof BasicDBObject) {
return (BasicDBObject) bson;
} else if (bson instanceof BsonDocument) {
return (BsonDocument) bson;
} else if (bson instanceof Document) {
return (Document) bson;
} else {
logger.debug("bson KV is null {} ", bson.getClass().getName());
return null;
}
//TODO leave comments for further use
// if(arg instanceof BsonDocumentWrapper) {
// bson.append(arg.toString());
// }
// if(arg instanceof CommandResult) {
// bson.append(arg.toString());
// }
// if(arg instanceof RawBsonDocument) {
// bson.append(arg.toString());
// }
}
static void writeFieldToDb(String range, BasicDBObject o, Annotation a,
String dbKey, Feature f) {
if (range.equals("String")) {
o.put(dbKey, a.getStringValue(f));
} else if (range.equals("StringArray")) {
StringArray sa = (StringArray) a.getFeatureValue(f);
if (sa != null) {
String[] vals = sa.toArray();
o.put(dbKey, Lists.newArrayList(vals));
}
} else if (range.equals("Integer")) {
o.put(dbKey, a.getIntValue(f));
} else if (range.equals("Float")) {
o.put(dbKey, a.getFloatValue(f));
} else if (range.equals("Boolean")) {
o.put(dbKey, a.getBooleanValue(f));
} else {
LOG.warn("range not supported " + range);
}
}
private Single<Map<Object, Object>> usersStatusRepartition(AnalyticsQuery query) {
return Observable.fromPublisher(usersCollection.aggregate(
Arrays.asList(
Aggregates.match(and(eq(FIELD_REFERENCE_TYPE, DOMAIN.name()), eq(FIELD_REFERENCE_ID, query.getDomain()))),
Aggregates.group(
new BasicDBObject("_id", query.getField()),
Accumulators.sum("total", 1),
Accumulators.sum("disabled", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$enabled", false)), 1, 0))),
Accumulators.sum("locked", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$and", Arrays.asList(new BasicDBObject("$eq", Arrays.asList("$accountNonLocked", false)), new BasicDBObject("$gte", Arrays.asList("$accountLockedUntil", new Date())))), 1, 0))),
Accumulators.sum("inactive", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$lte", Arrays.asList("$loggedAt", new Date(Instant.now().minus(90, ChronoUnit.DAYS).toEpochMilli()))), 1, 0)))
)
)))
.map(doc -> {
Long nonActiveUsers = ((Number) doc.get("disabled")).longValue() + ((Number) doc.get("locked")).longValue() + ((Number) doc.get("inactive")).longValue();
Long activeUsers = ((Number) doc.get("total")).longValue() - nonActiveUsers;
Map<Object, Object> users = new HashMap<>();
users.put("active", activeUsers);
users.putAll(doc.entrySet()
.stream()
.filter(e -> !"_id".equals(e.getKey()) && !"total".equals(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
return users;
})
.first(Collections.emptyMap());
}
/**
* Atomically modify and return a single document.
* @param id
* @param updater the modifications to apply
* @param returnNew when true, returns the modified document rather than the original
* @return
*/
public T findAndModify(String id, BuguUpdater updater, boolean returnNew){
DBObject query = new BasicDBObject();
query.put(Operator.ID, IdUtil.toDbId(clazz, id));
DBObject result = getCollection().findAndModify(query, null, null, false, updater.getModifier(), returnNew, false);
T t = MapperUtil.fromDBObject(clazz, result);
if(hasCustomListener){
if(returnNew){
notifyUpdated((BuguEntity)t);
}else{
BuguEntity entity = (BuguEntity)findOne(id);
notifyUpdated(entity);
}
}
return t;
}
private List<Seq> keywordInSequence(String in_field, String keyword) {
List<Seq> seqs = new ArrayList<Seq>();
BasicDBObject query = new BasicDBObject();
query.put(in_field, keyword);
BasicDBObject keys = new BasicDBObject();
DBCursor cur = this.dbSeq.find(query, keys);
while (cur.hasNext()) {
DBObject o = cur.next();
seqs.add( convertDBObjectToSeq(o) );
}
cur.close();
return seqs;
}
public List<Seq> getSeqWithRxnRef(Long rxnId) {
List<Seq> seqs = new ArrayList<>();
BasicDBObject query = new BasicDBObject();
query.put("rxn_refs", rxnId);
DBCursor cur = this.dbSeq.find(query, new BasicDBObject());
try {
while (cur.hasNext()) {
DBObject o = cur.next();
seqs.add(convertDBObjectToSeq(o));
}
} finally {
if (cur != null) {
cur.close();
}
}
return seqs;
}
@Test
public void testUpdateRetry() {
TenantContext.setTenantId("SLIUnitTest");
repository.deleteAll("student", null);
DBObject indexKeys = new BasicDBObject("body.cityOfBirth", 1);
mongoTemplate.getCollection("student").ensureIndex(indexKeys);
repository.create("student", buildTestStudentEntity());
Entity entity = repository.findOne("student", new NeutralQuery());
Map<String, Object> studentBody = entity.getBody();
studentBody.put("cityOfBirth", "ABC");
Entity studentEntity = new MongoEntity("student", entity.getEntityId(), studentBody,
entity.getMetaData());
repository.updateWithRetries("student", studentEntity, 5);
NeutralQuery neutralQuery = new NeutralQuery();
neutralQuery.addCriteria(new NeutralCriteria("cityOfBirth=ABC"));
assertEquals(1, repository.count("student", neutralQuery));
repository.deleteAll("student", null);
mongoTemplate.getCollection("student").dropIndex(indexKeys);
}
public BasicDBObject getSearchQuery(final List<String> searchWords, final Coordinates coordinates, final String[] searchCategories) {
final List<BasicDBObject> searchParameters = new ArrayList<>();
for (int i = 0; i < searchWords.size(); i++) {
String key = searchWords.get(i);
if (!key.equals("")) {
Pattern matchPattern;
try {
matchPattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
} catch (final PatternSyntaxException e) {
key = Pattern.quote(key);
searchWords.set(i, key);
matchPattern = Pattern.compile(key);
}
for (final String searchCategory : searchCategories) {
searchParameters.add(new BasicDBObject(searchCategory, matchPattern));
}
}
}
return coordinates.getQueryObject().append("$or", searchParameters);
}
@Override
public void delete(WaveletName waveletName) throws PersistenceException,
FileNotFoundPersistenceException {
BasicDBObject criteria = new BasicDBObject();
criteria.put(MongoDbDeltaStoreUtil.FIELD_WAVE_ID, waveletName.waveId.serialise());
criteria.put(MongoDbDeltaStoreUtil.FIELD_WAVELET_ID, waveletName.waveletId.serialise());
try {
// Using Journaled Write Concern
// (http://docs.mongodb.org/manual/core/write-concern/#journaled)
deltasCollection.withWriteConcern(WriteConcern.JOURNALED).deleteMany(criteria);
} catch (MongoException e) {
throw new PersistenceException(e);
}
// Also delete wavelet snapshots
snapshotStore.deleteSnapshot(waveletName);
}
@Override
public List<BarField> queryTodayBar5MinList(long startTimestamp, long endTimestamp, String unifiedSymbol) {
try {
Document filter = new Document();
Document dateDocument = new Document();
dateDocument.put("$gte", startTimestamp);
dateDocument.put("$lte", endTimestamp);
filter.put("actionTimestamp", dateDocument);
filter.put("unifiedSymbol", unifiedSymbol);
BasicDBObject sortBO = new BasicDBObject();
sortBO.put("actionTimestamp", 1);
long beginTime = System.currentTimeMillis();
List<Document> documentList = this.todayMarketDataDBClient.find(todayMarketDataDBName, COLLECTION_NAME_BAR_5_MIN, filter, sortBO);
logger.info("查询Bar数据,数据库{},集合{},操作耗时{}ms,共{}条数据", todayMarketDataDBName, COLLECTION_NAME_BAR_5_MIN, (System.currentTimeMillis() - beginTime), documentList.size());
return documentListToBarList(documentList, MarketDataDBTypeEnum.MDDT_TD.getValueDescriptor().getName());
} catch (Exception e) {
logger.error("查询当日5分钟数据发生错误", e);
}
return new ArrayList<>();
}
public Object insert(DBCollection collection, WriteConcern writeConcern) {
DBObject document = new BasicDBObject();
// 匹配_id
for (int i = 0, n = columns.size(); i < n; i++) {
// document.put(columns.get(i), values.get(i).getValue());
String tempColumn = columns.get(i);
if (3 == tempColumn.length() && tempColumn.equals("_id")) {
document.put(tempColumn, new ObjectId(values.get(i).getValue().toString()));
} else {
document.put(tempColumn, values.get(i).getValue());
}
}
log(document);
// TODO: WriteConcern.ACKNOWLEDGED需要可以配置
// WriteResult result = collection.insert(document, WriteConcern.ACKNOWLEDGED);
// collection.insert(document, MongoComponent.getInstance().getDefaultWriteConcern());
collection.insert(document, writeConcern);
Object oid = document.get("_id");
if (null != oid) {
return oid.toString();
}
return null;
}
@Test
public void testMongodbLocalServer() throws Exception {
MongoClient mongo = new MongoClient(mongodbLocalServer.getIp(), mongodbLocalServer.getPort());
DB db = mongo.getDB(propertyParser.getProperty(ConfigVars.MONGO_DATABASE_NAME_KEY));
DBCollection col = db.createCollection(propertyParser.getProperty(ConfigVars.MONGO_COLLECTION_NAME_KEY),
new BasicDBObject());
col.save(new BasicDBObject("testDoc", new Date()));
LOG.info("MONGODB: Number of items in collection: {}", col.count());
assertEquals(1, col.count());
DBCursor cursor = col.find();
while(cursor.hasNext()) {
LOG.info("MONGODB: Document output: {}", cursor.next());
}
cursor.close();
}
/**
* At setup time, init last completed windowId from maxWindowTable
*
* @param context
*/
@Override
public void setup(OperatorContext context)
{
operatorId = context.getId();
try {
mongoClient = new MongoClient(hostName);
db = mongoClient.getDB(dataBase);
if (userName != null && passWord != null) {
db.authenticate(userName, passWord.toCharArray());
}
initLastWindowInfo();
for (String table : tableList) {
tableToDocumentList.put(table, new ArrayList<DBObject>());
tableToDocument.put(table, new BasicDBObject());
}
} catch (UnknownHostException ex) {
logger.debug(ex.toString());
}
}
public void updateDatabase(DBCollection reachables) {
for (String product : products) {
// The query object for this product
BasicDBObject newProductQuery = new BasicDBObject().append(INCHI_KEY, product);
// DB list of the substrates of this projection
BasicDBList substrateList = new BasicDBList();
substrateList.addAll(substrates);
// DB list of the one RO associated with this projection
BasicDBList roList = new BasicDBList();
roList.addAll(ros);
// The full entry to be added to the product's precursor list
BasicDBObject precursorEntry = new BasicDBObject()
.append(SUBSTRATES_KEY, substrateList)
.append(RO_KEY, roList);
// The command to push the precursor entry onto the precursor list
BasicDBObject precursors = new BasicDBObject();
precursors.append("$push", new BasicDBObject(PRECURSOR_KEY, precursorEntry));
// Do the update!
reachables.update(newProductQuery, precursors, UPSERT, NO_MULTI);
}
}
/**
* 根据oid去数据库回查数据
*
* @param oid
* @return
*/
private Document fetchData(String schemaName, String tableName, String oid) {
Document result = null;
DbusDatasource datasource = GlobalCache.getDatasource();
MongoClientURI uri = new MongoClientURI(datasource.getMasterUrl());
MongoClient client = new MongoClient(uri);
MongoDatabase database = client.getDatabase(schemaName);
MongoCollection<Document> collection = database.getCollection(tableName);
MongoCursor<Document> cursor = collection.find(new BasicDBObject().append("_id", new ObjectId(oid))).iterator();
if (cursor.hasNext()) {
result = cursor.next();
} else {
logger.error("get source data error. schemaName:{}, tableName:{}, oid:{}", schemaName, tableName, oid);
}
client.close();
return result;
}
private static void validateAggregateCommand( DBObject commandObj ) throws OdaException
{
// validate a $group pipeline operation expression, if specified
List<BasicDBObject> groupOps = findPipelineOperation( commandObj, GROUP_AGGR_KEY );
for( BasicDBObject groupOp : groupOps )
{
if( ! groupOp.containsField( DOC_ID_FIELD_NAME ) )
throw new OdaException( Messages.bind( Messages.queryModel_missingGroupAggrKey, new Object[]{ GROUP_AGGR_KEY, DOC_ID_FIELD_NAME, groupOp } ) );
}
// validate a $sort pipeline operation expression, if specified
List<BasicDBObject> sortOps = findPipelineOperation( commandObj, SORT_AGGR_KEY );
for( BasicDBObject sortOp : sortOps )
{
for( Object sortKeySpec : sortOp.values() )
{
if( sortKeySpec instanceof Number )
{
int sortKeyValue = ((Number)sortKeySpec).intValue();
if( sortKeyValue == 1 || sortKeyValue == -1 )
continue; // is valid
}
throw new OdaException( Messages.bind( Messages.queryModel_invalidSortAggrValue, SORT_AGGR_KEY, sortOp ) );
}
}
}
/**
* checks if an entry exists in the dbName DB
* @param entry to check
* @param dbName to check in
* @return true if new
*/
public static boolean isNew(DBEntry entry, String dbName){
if(entry == null)
return false;
MongoDatabase db = MongoDB.INSTANCE.getDatabase(dbName);
String collectionName = getCollectionName(entry);
MongoCollection collection = db.getCollection(collectionName,BasicDBObject.class);
BasicDBObject obj = (BasicDBObject) collection.find(eq("_id", entry.getId())).first();
if(obj != null)
return false;
else
return true;
}
/**
* Create a Mongo DB Object baed on the content of this group
*
* @param id The Mongo Object id to assign to this DB Object. If null, a new Object id will be
* created
* @return - The Mongo DB Object based on the content of this group
*/
public BasicDBObject getDBObject(boolean includeId) {
BasicDBObject group = new BasicDBObject();
if (includeId) {
group.append(DB_ID, new ObjectId(id));
}
group.append(JSON_KEY_GROUP_NAME, name);
BasicDBList membersArray = new BasicDBList();
for (int i = 0; i < members.length; i++) {
membersArray.add(members[i]);
}
group.append(JSON_KEY_MEMBERS_LIST, membersArray);
return group;
}
/**
* mark the book as cheap or expensive.
*/
//@Test
public void testCond(){
connectDB();
BookDao dao = new BookDao();
BuguAggregation agg = dao.aggregate();
DBObject cond = ExpressionBuilder.cond().ifCondition("{'$lt':['$price', 10]}").thenValue("cheap").elseValue("expensive").build();
DBObject p = new BasicDBObject();
p.put("title", 1);
p.put("price", cond);
Iterable<DBObject> it = agg.project(p).results();
for(DBObject dbo : it){
System.out.print(dbo.get("title"));
System.out.print(" : ");
System.out.println(dbo.get("price"));
}
disconnectDB();
}
@Override
public void concretProcessor(Object expKey, Map expValue, BasicDBObject set) {
Map KVMap = (Map) expValue.get(expKey);
Set keyset = KVMap.keySet();
Iterator iter = keyset.iterator();
String key = null;
BasicDBObject content = new BasicDBObject();
while (iter.hasNext()) {
key = (String) iter.next();
content.append(key, KVMap.get(key));
}
switch (expKey.toString()) {
case "set":
set.append("$set", content);
break;
case "unset":
set.append("$unset", content);
break;
case "rename":
set.append("$rename", content);
break;
case "push":
set.append("$push", content);
break;
case "pull":
set.append("$pull", content);
break;
default:
set.append("$" + expKey.toString(), content);
break;
}
log.info(this, "set :" + set.toJson());
}
public Operator elemMatch(Operator... operators) {
BasicDBObject _dbObj = new BasicDBObject();
for (Operator _opt : operators) {
_dbObj.putAll((BSONObject) _opt.toBson());
}
__doAddOperator(IMongo.OPT.ELEM_MATCH, _dbObj);
return this;
}
/**
* 将传入的值<code>value</code>转换成对应的类型<code>type</code>返回。
*
* @param columnLabel 列名
* @param value 值
* @param type 对应的类型
* @return 转换后的对象
*/
public static Object valueMapper(String columnLabel, Object value, Class<?> type) {
if (value == null) {
return null;
}
// mongodb _id field
if (type.isAssignableFrom(ObjectId.class)
&& (value instanceof ObjectId || value instanceof String)) {
return new ObjectId(value.toString());
}
// enum
if (type.isEnum()) {
return value.toString();
}
// embedded collection,内嵌集合
if ((type.isAssignableFrom(List.class) || type.isAssignableFrom(Set.class))
&& value instanceof BasicDBList) {
// TODO 拿不到范型,list没法转
LOG.debug("column:[{}],type:[{}]为内嵌列表,无法获取范型类,无法映射.return null.", columnLabel, type);
return null;
}
// embedded object,内嵌对象
if (value instanceof BasicDBObject) {
BasicDBObject dbObj = (BasicDBObject) value;
return beanMapper(dbObj, type);
}
// embedded array,内嵌数组
if (type.isArray() && value instanceof BasicDBList) {
BasicDBList basicDBList = (BasicDBList) value;
return arrayMapper(basicDBList, type);
}
LOG.debug("column:[{}],type:[{}] unsupported type yet.return null", columnLabel, type);
return null;
}
public static List<Contribution> dbListToList(BasicDBList dbl) {
String method = "dbListToList";
logger.entering(clazz, method, dbl);
List<Contribution> contributions = new ArrayList<>();
for (Object dbo : ListUtils.emptyIfNull(dbl)) {
contributions.add(new Contribution((BasicDBObject) dbo));
}
logger.exiting(clazz, method, listToString(contributions));
return contributions;
}
/**
* This function retrieves the chemical corresponding to a ChEBI ID and update its metadata with the ChEBI
* applications provided
* @param chebiId ChEBI ID for the chemical to update
* @param applicationSet Set of main and direct ChEBI applications, represented in a ChebiApplicationSet
*/
public void updateChemicalWithChebiApplications(String chebiId,
BrendaChebiOntology.ChebiApplicationSet applicationSet) {
Chemical c = this.getChemicalFromChebiId(chebiId);
if (c != null && applicationSet != null) {
long id = c.getUuid();
BasicDBObject query = new BasicDBObject("_id", id);
BasicDBObject update = new BasicDBObject("$set",
new BasicDBObject("xref.CHEBI.metadata.applications",
applicationSet.toBasicDBObject()));
this.dbChemicals.update(query, update);
}
}
@Test
public void basicTest() throws Exception {
fongo.getDB("test").getCollection("test").insert(
new BasicDBObject().
append("_id", "documentPrimaryKey").
append("fieldToKeep1", "initialvalue1").
append("fieldToKeep2", "initialvalue2").
append("fieldToUpdate", "toupdate")
);
Transaction tx = channel.getTransaction();
tx.begin();
Map<String, String> headers = new HashMap<String, String>();
headers.put("_id", "documentPrimaryKey");
headers.put("fieldToUpdate", "updated");
headers.put("newField", "added");
Event event = EventBuilder.withBody(new byte[0], headers);
channel.put(event);
tx.commit();
tx.close();
mongoSink.process();
DBObject result = fongo.getDB("test").getCollection("test").findOne();
assertThat(result.get("_id")).isEqualTo("documentPrimaryKey");
assertThat(result.get("fieldToKeep1")).isEqualTo("initialvalue1");
assertThat(result.get("fieldToKeep2")).isEqualTo("initialvalue2");
assertThat(result.get("fieldToUpdate")).isEqualTo("updated");
assertThat(result.get("newField")).isEqualTo("added");
}
/** Tests login via the non-SSL port. The connection should be denied or forwarded. */
@Test
public void testLoginNonSsl() throws Exception {
// Add a user.
String loginAuthHeader =
"Bearer "
+ new JWTVerifier()
.createJWT("unauthenticated", new HashSet<String>(Arrays.asList("login")));
User user =
new User(null, "Niels", "Bohr", "nBohr", "@nBohr", "nBohrWishListLink", "myPassword");
Response response = processRequest(userServiceURL, "POST", user.getJson(), loginAuthHeader);
assertEquals(
"HTTP response code should have been " + Status.OK.getStatusCode() + ".",
Status.OK.getStatusCode(),
response.getStatus());
String authHeader = response.getHeaderString("Authorization");
new JWTVerifier().validateJWT(authHeader);
JsonObject responseJson = toJsonObj(response.readEntity(String.class));
String dbId = responseJson.getString(User.JSON_KEY_USER_ID);
user.setId(dbId);
// Find user in the database.
BasicDBObject dbUser =
(BasicDBObject) database.getCollection("users").findOne(new ObjectId(dbId));
assertTrue("User rFeynman was NOT found in database.", dbUser != null);
assertTrue("User rFeynman does not contain expected data.", user.isEqual(dbUser));
// Test 1: Login the user on non-ssl port. We should be redirected to
// the SSL port.
String postUrl = "http://" + libertyHostname + ":" + libertyPort + "/logins";
JsonObjectBuilder loginPayload = Json.createObjectBuilder();
loginPayload.add(User.JSON_KEY_USER_NAME, user.userName);
loginPayload.add(User.JSON_KEY_USER_PASSWORD, user.password);
response = processRequest(postUrl, "POST", loginPayload.build().toString(), authHeader);
assertEquals("HTTP response code should have been 302.", 302, response.getStatus());
}
public NewsgroupArticlesData findOneByNewsgroupName(String q) {
NewsgroupArticlesData newsgroupArticlesData = (NewsgroupArticlesData) PongoFactory.getInstance().createPongo(dbCollection.findOne(new BasicDBObject("newsgroupName", q + "")));
if (newsgroupArticlesData != null) {
newsgroupArticlesData.setPongoCollection(this);
}
return newsgroupArticlesData;
}
@Test
public void insertTweetToDataBase() {
String data = "Исламын бүлэглэлүүд нь Узбек, Тажик, Киргиз улсыг хамарсан Ферганын хөндийд байрлаж байна";
String url = "http://twitter.com/sukhee56/status/640834315731910656";
Tweet tweet = new Tweet(data,data,"tst","sukhee56",null,"topsy",url,"640834315731910656","filename");
tweets.insertOne(tweet);
BasicDBObject obj = (BasicDBObject) tweets.find(eq("data", data)).first();
Tweet tweet2 = new Tweet(obj);
assertNotNull(tweet.equals(tweet2));
}
public LibreWifiData(BasicDBObject src) {
BlockBytes = src.getString("BlockBytes");
CaptureDateTime = src.getLong("CaptureDateTime");
ChecksumOk = src.getInt("ChecksumOk");
DebugInfo = src.getString("DebugInfo");
TomatoBatteryLife = src.getInt("TomatoBatteryLife");
UploaderBatteryLife = src.getInt("UploaderBatteryLife");
Uploaded = src.getInt("Uploaded");
HwVersion = src.getString("HwVersion");
FwVersion = src.getString("FwVersion");
SensorId = src.getString("SensorId");
patchUid = src.getString("patchUid");
patchInfo = src.getString("patchInfo");
}
@DELETE
@Path("/delete/{product}/{version}")
@Produces(MediaType.APPLICATION_JSON)
public Response softDeleteSingleVersion(@PathParam("product") final String product,
@PathParam("version") final String version) {
final DBCollection collection = this.mongoLegacyDb.getCollection("summary");
final DBCollection targetCollection = this.mongoLegacyDb.getCollection("deletedSummary");
final Pattern productReg = java.util.regex.Pattern.compile("^" + product + "/" + version + "$");
final BasicDBObject query = new BasicDBObject("_id", productReg);
final DBCursor cursor = collection.find(query);
DBObject doc;
while (cursor.hasNext()) {
doc = cursor.next();
// kill the old id
doc.removeField("_id");
try {
targetCollection.insert(doc);
} catch (final Throwable e) {
return Response.status(500).build();
}
}
collection.remove(query);
return Response.ok().build();
}