下面列出了怎么用com.mongodb.DBCollection的API类实例代码及写法,或者点击链接到github查看源代码。
@ChangeSet(order = "01",
author = "developer",
id = "01-addUsers")
public void addUsers(final DB db) {
final DBCollection userCollection = db.getCollection(User.COLLECTION_NAME);
userCollection.insert(BasicDBObjectBuilder
.start()
.add(FIELD_NAME_ID, new ObjectId("590f86d92449343841cc2c3f"))
.add(User.FIELD_NAME_FIRST_NAME, "User")
.add(User.FIELD_NAME_LAST_NAME, "One")
.add(User.FIELD_NAME_EMAIL, "[email protected]")
.get());
userCollection.insert(BasicDBObjectBuilder
.start()
.add(FIELD_NAME_ID, new ObjectId("590f86d92449343841cc2c40"))
.add(User.FIELD_NAME_FIRST_NAME, "User")
.add(User.FIELD_NAME_LAST_NAME, "Two")
.add(User.FIELD_NAME_EMAIL, "[email protected]")
.get());
}
/**
* Create a neutral record from a dbElement of a base collection.
* Initiates recursive parsing of sub tables filling the neutralRecord map.
*/
private NeutralRecord createNeutralRecord(DBObject dbElement, String entityName,
List<DBCollection> supportingCollections) {
NeutralRecord record = new NeutralRecord();
record.setRecordType(entityName);
int joinKey = Integer.parseInt(dbElement.get("JoinKey").toString());
try {
Map<String, Object> attributes = parseDbElement(dbElement, joinKey, entityName, supportingCollections);
record.setAttributes(attributes);
} catch (Exception e) {
e.printStackTrace();
PRINT_STREAM.println("invalid collection format for entity type " + entityName);
}
return record;
}
/**
* Find a DBObject from database using the query. If the ‘fields' argument is null,
* then return the whole document. Otherwise, only returns given fields.
*
* This method uses reflection to convert the result DBObject into given Object.
*
* @param query The query condition
* @param databaseName The database name
* @param namespace The collection namespace
* @param collection The collection name
* @param filterFields The fields that will be returned.
* @return
*/
public static final Object queryObjectFromMongo(DBObject query, String databaseName,
String namespace, String collection, DBObject filterFields) {
DBCollection coll = getDBCollection(databaseName, namespace, collection);
DBObject dbObject = null;
if ( filterFields != null ) {
dbObject = coll.findOne(query, filterFields);
} else {
dbObject = coll.findOne(query);
}
if ( dbObject != null ) {
return constructObject(dbObject);
} else {
return null;
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/featureTagIndex/{product}/{major}.{minor}.{servicePack}/{build}")
public Response getFeatureTagIndexForReport(@BeanParam final Coordinates coordinates,
@QueryParam("searchText") final String searchText, @QueryParam("viewPassed") final Integer viewPassed,
@QueryParam("viewFailed") final Integer viewFailed,
@QueryParam("viewUndefined") final Integer viewUndefined, @QueryParam("viewSkipped") final Integer viewSkipped,
@QueryParam("start") final String start) {
final DBCollection featuresCollection = this.mongoLegacyDb.getCollection("features");
final BasicDBObject query = QueryBuilder.getInstance().buildFilterQuery(coordinates, searchText, viewPassed,
viewFailed, viewUndefined, viewSkipped, start);
query.append("$and", QueryBuilder.getInstance().buildHasTagsQuery());
final DBCursor results = featuresCollection.find(query,
new BasicDBObject("tags", 1).append("elements.tags", 1).append("name", 1).append("calculatedStatus", 1)
.append("id", 1).append("elements.steps", 1).append("elements.name", 1).append("elements.id", 1));
return Response.ok(SerializerUtil.serialise(getTagList(results))).build();
}
public void testFindOne() throws UnknownHostException {
MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print");
DBCollection collection = client.getCollection("marc");
BasicDBObject doc = createTestObject();
collection.insert(doc);
assertEquals(1, collection.count());
DBObject myDoc = collection.findOne();
assertEquals("MongoDB", myDoc.get("name"));
assertEquals("database", myDoc.get("type"));
assertEquals(1, myDoc.get("count"));
assertEquals(BasicDBObject.class, myDoc.get("info").getClass());
assertEquals(new BasicDBObject("x", 203).append("y", 102), myDoc.get("info"));
assertEquals(203, ((BasicDBObject)myDoc.get("info")).get("x"));
assertEquals(Integer.class, ((BasicDBObject)myDoc.get("info")).get("x").getClass());
System.out.println(myDoc);
collection.remove(new BasicDBObject("name", "MongoDB"));
}
/**
* Gets shards.
*
* @param collection the collection
* @return the shards
*/
private Map<String, String[]> getShards(DBCollection collection) {
DB config = collection.getDB().getSisterDB("config");
DBCollection configShards = config.getCollection("shards");
DBCursor cursorShards = configShards.find();
Map<String, String[]> map = new HashMap<>();
while (cursorShards.hasNext()) {
DBObject currentShard = cursorShards.next();
String currentHost = (String) currentShard.get("host");
int slashIndex = currentHost.indexOf("/");
if (slashIndex > 0) {
map.put((String) currentShard.get(MONGO_DEFAULT_ID),
currentHost.substring(slashIndex + 1).split(","));
}
}
return map;
}
@Test
public void saveIndexAndFeatureByAppIdTest() throws Exception{
String idApplication = "1";
//Fixture
Feature feature=Mockito.mock(Feature.class);
DBCollection dbCollection=Mockito.mock(DBCollection.class);
BasicDBObject obj=Mockito.mock(BasicDBObject.class);
Mockito.when(mongoTemplate.collectionExists(collectionName+idApplication)).thenReturn(false);
Mockito.when(mongoTemplate.getCollection(collectionName+idApplication)).thenReturn(dbCollection);
PowerMockito.whenNew(BasicDBObject.class).withNoArguments().thenReturn(obj);
Mockito.when(obj.put("geometry", "2dsphere")).thenReturn(dbCollection);
//Experimentation
featureRepository.save(idApplication, feature);
//Expectation
Mockito.verify(mongoTemplate).collectionExists(collectionName+idApplication);
Mockito.verify(mongoTemplate).save(feature,collectionName+idApplication);
}
public List<NeutralRecord> getNeutralRecordsFromCollection(String entityName) {
PRINT_STREAM.println("importing from collection " + entityName);
List<NeutralRecord> records = new ArrayList<NeutralRecord>();
// Get a list of all the collections in the staging DB
Set<String> allCollections = db.getCollectionNames();
List<DBCollection> dbSupportingCollections = new ArrayList<DBCollection>();
Iterator<String> it = allCollections.iterator();
while (it.hasNext()) {
dbSupportingCollections.add(db.getCollection(it.next().toString()));
}
// Get the data in the primary (entityName) collection.
DBCollection dbCollection = db.getCollection(entityName);
DBCursor cursor = dbCollection.find();
// Create the neutral record on a entry-by-entry basis
while (cursor.hasNext()) {
records.add(createNeutralRecord(cursor.next(), entityName, dbSupportingCollections));
}
return records;
}
public void pushSample(DBObject sample, boolean isList, BasicDBList resultList) {
if(isList){
// Use the batch API to send a number of samples
storeBatch((BasicDBList)sample, resultList);
}
else if(sample != null){
// Create an oid to embed the sample time
BasicDBObject doc = ((BasicDBObject) sample);
SampleId _id = this.idFactory.createId(sample);
sample.put(Sample.ID_KEY, _id.toObject());
resultList.add(_id.toObject());
// Get the correct slice from the allocator and insert
long timestamp = doc.getLong(Sample.TS_KEY);
DBCollection collection = collectionAllocator.getCollection(timestamp);
collection.insert(doc);
}
}
private DBCursor createCursor() {
DBCollection oplog = _mongo.getDB("local").getCollection("oplog.rs");
BSONTimestamp startingTimestamp = getStartingTimestamp();
DBCursor cursor;
if (startingTimestamp == null) {
log.info("Tailing the oplog from the beginning...");
cursor = oplog.find();
} else {
log.info("Tailing the oplog from " + startingTimestamp);
BasicDBObject query = new BasicDBObject("ts", new BasicDBObject("$gt", startingTimestamp));
cursor = oplog.find(query);
cursor.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
}
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
return cursor;
}
@Test
@DisplayName("when valid cdc event with single field PK then correct DeleteOneModel")
public void testValidSinkDocumentSingleFieldPK() {
BsonDocument filterDoc =
new BsonDocument(DBCollection.ID_FIELD_NAME,
new BsonDocument("id",new BsonInt32(1004)));
BsonDocument keyDoc = new BsonDocument("id",new BsonInt32(1004));
BsonDocument valueDoc = new BsonDocument("op",new BsonString("d"));
WriteModel<BsonDocument> result =
RDBMS_DELETE.perform(new SinkDocument(keyDoc,valueDoc));
assertTrue(result instanceof DeleteOneModel,
() -> "result expected to be of type DeleteOneModel");
DeleteOneModel<BsonDocument> writeModel =
(DeleteOneModel<BsonDocument>) result;
assertTrue(writeModel.getFilter() instanceof BsonDocument,
() -> "filter expected to be of type BsonDocument");
assertEquals(filterDoc,writeModel.getFilter());
}
@PUT
@Path("/ignoredTags/{product}")
@Consumes(MediaType.APPLICATION_JSON)
public Response putIgnoredTags(@BeanParam final Coordinates coordinates, final BasicDBObject patch) {
final DBCollection collection = this.mongoLegacyDb.getCollection("ignoredTags");
final BasicDBObject coq = coordinates.getProductCoordinatesQueryObject();
final BasicDBObject storedDocument = (BasicDBObject) collection.findOne(coq);
final String tagName = (String) patch.get("tagName");
if (storedDocument != null) {
final BasicDBObject documentToUpdate = (BasicDBObject) storedDocument.copy();
updateIgnoredTag(documentToUpdate, tagName);
collection.save(documentToUpdate);
} else {
final DBObject newDocument = generateNewIgnoredTags(coordinates, tagName);
collection.save(newDocument);
}
return Response.ok().build();
}
@Test
public void testPersist() {
DBCollection coll = createMockCollection();
ToDo td = new ToDo();
td.setTitle("This is a test");
td.setId("aaaaaaaaaaaaaaaaaaaaaaa1");
expect(coll.insert(isA(DBObject.class))).andAnswer(new IAnswer<WriteResult>() {
@Override
public WriteResult answer() throws Throwable {
DBObject obj = (DBObject)getCurrentArguments()[0];
obj.put("_id", new ObjectId("aaaaaaaaaaaaaaaaaaaaaaa1"));
return null;
}
});
replay(coll);
MongoStore store = new MongoStore(coll);
assertEquals(td, store.persist(td));
verify(coll);
}
/**
* Execute a query on (mongo) database
*
* @param query path to query
* @param type type of data in query (string or json)
* @param collection collection in database
* @param modifications modifications to perform in query
*/
@When("^I execute a query '(.+?)' of type '(json|string)' in mongo '(.+?)' database using collection '(.+?)' with:$")
public void sendQueryOfType(String query, String type, String database, String collection, DataTable modifications) throws Exception {
try {
commonspec.setResultsType("mongo");
String retrievedData = commonspec.retrieveData(query, type);
String modifiedData = commonspec.modifyData(retrievedData, type, modifications);
commonspec.getMongoDBClient().connectToMongoDBDataBase(database);
DBCollection dbCollection = commonspec.getMongoDBClient().getMongoDBCollection(collection);
DBObject dbObject = (DBObject) JSON.parse(modifiedData);
DBCursor cursor = dbCollection.find(dbObject);
commonspec.setMongoResults(cursor);
} catch (Exception e) {
commonspec.getExceptions().add(e);
}
}
@Test(expected=MongoGeospatialException.class)
public void shouldThrowErrorWhenGeospatialIndexNotCreated() throws Exception{
String idApplication = "1";
//Fixture
Feature feature=Mockito.mock(Feature.class);
DBCollection dbCollection=Mockito.mock(DBCollection.class);
BasicDBObject obj=Mockito.mock(BasicDBObject.class);
Mockito.when(mongoTemplate.collectionExists(collectionName+idApplication)).thenReturn(false);
Mockito.when(mongoTemplate.getCollection(collectionName+idApplication)).thenReturn(dbCollection);
PowerMockito.whenNew(BasicDBObject.class).withNoArguments().thenReturn(obj);
Mockito.doThrow(UncategorizedMongoDbException.class).when(mongoTemplate).save(feature,collectionName+idApplication);
//Experimentation
featureRepository.save(idApplication, feature);
//Expectation
Mockito.verify(mongoTemplate).collectionExists(collectionName+idApplication);
Mockito.verify(mongoTemplate).save(feature,collectionName+idApplication);
}
private Map<String, Float> readDistanceScores(String object) {
Map<String, Float> result = new HashMap<String, Float>();
Query query = new org.springframework.data.mongodb.core.query.Query();
query.addCriteria(Criteria.where("type.name").is(sm.getSimilarityName())
.orOperator(Criteria.where("fromArtifact.$id").is(new ObjectId(object)),
Criteria.where("toArtifact.$id").is(new ObjectId(object))));
DBCollection dbCollection = mongoTemplate.getCollection("relation");
DBCursor cursor = dbCollection.find(query.getQueryObject());
List<DBObject> list = cursor.toArray();
for (DBObject dbObject : list) {
String toArtifact = ((DBRef)dbObject.get("toArtifact")).getId().toString();
String fromArtifact = ((DBRef)dbObject.get("fromArtifact")).getId().toString();
double value = ((double)dbObject.get("value"));
if (toArtifact.equals(object))
result.put(fromArtifact, (float) (1 - value));
else
result.put(toArtifact, (float) (1 - value));
}
return result;
}
private ArrayNode getHistoricDocuments(DBCollection dbCollection, DBObject query) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode nodeArray = mapper.createArrayNode();
DBCursor cursor = dbCollection.find(query);
while(cursor.hasNext()) {
DBObject obj = cursor.next();
JsonNode json;
try {
json = mapper.readTree(obj.toString());
nodeArray.add(json);
} catch (IOException e) {
e.printStackTrace();
}
}
return nodeArray;
}
@GET
@Path("/{product}/{major}.{minor}.{servicePack}/{build}")
@Produces(MediaType.APPLICATION_JSON)
public Response getSearchResults(@BeanParam final Coordinates coordinates, @QueryParam("keywords") final String keyword) {
final String[] searchCategories = { "name", "description", "tags.name", "elements.name", "elements.description",
"elements.steps.name", "elements.tags.name" };
final List<String> searchWords = Arrays.asList(keyword.split("\\s+"));
final DBCollection collection = this.mongoLegacyDb.getCollection("features");
final List<DBObject> searchResults = new ArrayList<>();
final QueryBuilder queryBuilder = QueryBuilder.getInstance();
final DBCursor results = collection.find(queryBuilder.getSearchQuery(searchWords, coordinates, searchCategories));
while (results.hasNext()) {
final DBObject doc = results.next();
searchResults.add(doc);
}
searchResults.sort(new DBObjectComparator(searchWords));
while (searchResults.size() > SEARCH_LIMIT) {
searchResults.remove(searchResults.size() - 1);
}
final BasicDBList basicDBList = new BasicDBList();
basicDBList.addAll(searchResults);
return Response.ok(SerializerUtil.serialise(basicDBList)).build();
}
@Test
public void testDatatableWithoutRowDefinition() throws Exception {
DBCollection collection = ChartUtil.getCollection(mongo, "Epsilon","org.eclipse.scava.metricprovider.historic.commitsovertime.CommitsOverTimeHistoricMetricProvider");
JsonNode node = ChartUtil.loadJsonFile("data/commitsovertime.json");
ArrayNode vis = (ArrayNode) node.get("vis");
JsonNode datatable = vis.get(0).get("datatable");
Chart chart = ChartUtil.loadChart("charts/linechart.json");
ArrayNode table = chart.createDatatable(datatable, collection, null);
System.out.println(table);
}
/**
* Delete a user.
*
* @param id The ID of the user to delete.
* @return Nothing.
*/
@DELETE
@Path("/{id}")
public Response deleteUser(@PathParam("id") String id) {
// Validate the JWT. The JWT must be in the 'users' group. We do not check
// to see if the user is deleting their own profile.
try {
validateJWT(new HashSet<String>(Arrays.asList("users")));
} catch (JWTException jwte) {
return Response.status(Status.UNAUTHORIZED)
.type(MediaType.TEXT_PLAIN)
.entity(jwte.getMessage())
.build();
}
// Retrieve the user from the database.
DB database = mongo.getMongoDB();
DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
ObjectId dbId = new ObjectId(id);
DBObject dbUser = dbCollection.findOne(dbId);
// If the user did not exist, return an error. Otherwise, remove the user.
if (dbUser == null) {
return Response.status(Status.BAD_REQUEST).entity("The user name was not Found.").build();
}
dbCollection.remove(new BasicDBObject(User.DB_ID, dbId));
return Response.ok().build();
}
/**
* Retrieve a user's profile.
*
* @param id The ID of the user.
* @return The user's profile, as a JSON object. Private fields such as password and salt are not
* returned.
*/
@GET
@Path("/{id}")
@Produces("application/json")
public Response getUser(@PathParam("id") String id) {
// Validate the JWT. The JWT must belong to the 'users' or 'orchestrator' group.
// We do not check if the user is retrieving their own profile, or someone else's.
try {
validateJWT(new HashSet<String>(Arrays.asList("users", "orchestrator")));
} catch (JWTException jwte) {
return Response.status(Status.UNAUTHORIZED)
.type(MediaType.TEXT_PLAIN)
.entity(jwte.getMessage())
.build();
}
// Retrieve the user from the database.
DB database = mongo.getMongoDB();
DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
DBObject user = dbCollection.findOne(new ObjectId(id));
// If the user did not exist, return an error. Otherwise, only return the public
// fields (exclude things like the password).
if (user == null) {
return Response.status(Status.BAD_REQUEST).entity("The user not Found.").build();
}
JsonObject responsePayload = new User(user).getPublicJsonObject();
return Response.ok(responsePayload, MediaType.APPLICATION_JSON).build();
}
/**
* Get all user profiles.
*
* @return All user profiles (excluding private fields like password).
*/
@GET
@Produces("application/json")
public Response getAllUsers() {
// Validate the JWT. The JWT must be in the 'users' group.
try {
validateJWT(new HashSet<String>(Arrays.asList("users")));
} catch (JWTException jwte) {
return Response.status(Status.UNAUTHORIZED)
.type(MediaType.TEXT_PLAIN)
.entity(jwte.getMessage())
.build();
}
// Get all the users from the database, and add them to an array.
DB database = mongo.getMongoDB();
DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
DBCursor cursor = dbCollection.find();
JsonArrayBuilder userArray = Json.createArrayBuilder();
while (cursor.hasNext()) {
// Exclude all private information from the list.
userArray.add((new User(cursor.next()).getPublicJsonObject()));
}
// Return the user list to the caller.
JsonObjectBuilder responseBuilder = Json.createObjectBuilder().add("users", userArray.build());
return Response.ok(responseBuilder.build(), MediaType.APPLICATION_JSON).build();
}
@Override
public void pushSample(DBObject sample, boolean isList, BasicDBList resultIds) {
if(isList){
// Use the batch API to send a number of samples
updateBatch((BasicDBList)sample);
}
else if(sample != null){
// This is a document, place it straight in appropriate collection
BasicDBObject doc = ((BasicDBObject) sample);
long timestamp = this.rollupPeriod * (doc.getLong(Sample.TS_KEY) / this.rollupPeriod);
DBCollection collection = collectionAllocator.getCollection(timestamp);
// Ask the id allocator for the query
BasicDBObject query = this.idFactory.getQuery(sample.get(Sample.SOURCE_KEY), timestamp);
// Build the update clause using the ops list
BasicDBObject update = new BasicDBObject();
for(RollupOperation rollupOp : this.rollupOps){
DBObject updateClause = rollupOp.getUpdateClause(sample);
// Check for top level operators that already exist so they dont overwrite
for(String key : updateClause.keySet()){
BasicDBObject existingClause = (BasicDBObject) update.get(key);
if(existingClause != null){
// Merge the arguments to the top level op
existingClause.putAll((DBObject)updateClause.get(key));
} else {
update.put(key, updateClause.get(key));
}
}
}
collection.update(query, update, true, false);
}
}
public void testInsert() throws UnknownHostException {
MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print");
DBCollection collection = client.getCollection("marc");
BasicDBObject doc = createTestObject();
collection.insert(doc);
assertNotNull(collection);
assertEquals(1, collection.count());
collection.remove(new BasicDBObject("name", "MongoDB"));
assertEquals(0, collection.count());
}
public void remove(String collectionName, String id) {
DBObject query = new BasicDBObject();
query.put(ID, id);
DBCollection collection = mongoPrivateStorageTemplate.getCollection(collectionName);
collection.remove(query);
}
@DELETE
@Path("/delete/{product}")
@Produces(MediaType.APPLICATION_JSON)
public Response softDeleteEntireProduct(@PathParam("product") final String product) {
final DBCollection collection = this.mongoLegacyDb.getCollection("summary");
final DBCollection targetCollection = this.mongoLegacyDb.getCollection("deletedSummary");
final BasicDBObject query = new BasicDBObject("coordinates.product", product);
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();
}
/**
* Remove the given collection from database.
* @param databaseName
* @param namespace
* @param collection
*/
public static final void removeDocument(String databaseName,
String namespace, String collection, DBObject query) {
DBCollection coll = getDBCollection(databaseName, namespace, collection);
if ( query == null ) {
query = MongoDBUtil.createDBObject();
}
coll.remove(query);
}
@Test
public void testUpdate() {
DBCollection coll = createMockCollection();
DBCursor cursor = createMockCursor();
DBObject query = new BasicDBObject("_id", new ObjectId("aaaaaaaaaaaaaaaaaaaaaaa2"));
DBObject dbObj = new BasicDBObject();
dbObj.put("_id", new ObjectId("aaaaaaaaaaaaaaaaaaaaaaa2"));
dbObj.put("title", "new title");
dbObj.put("completed", true);
dbObj.put("order", 0);
expect(cursor.next()).andReturn(dbObj);
cursor.close();
expectLastCall();
replay(cursor);
expect(coll.find(eq(query))).andReturn(cursor);
ToDo newTd = new ToDo();
newTd.setId("aaaaaaaaaaaaaaaaaaaaaaa2");
newTd.setCompleted(true);
newTd.setTitle("new title");
newTd.setOrder(0);
expect(coll.update(eq(query), eq(dbObj))).andReturn(null);
replay(coll);
MongoStore store = new MongoStore(coll);
assertEquals(newTd, store.update("aaaaaaaaaaaaaaaaaaaaaaa2", newTd));
verify(cursor);
verify(coll);
}
@Override
public boolean createQueue(String jobClientNodeGroup) {
String tableName = JobQueueUtils.getFeedbackQueueName(jobClientNodeGroup);
DBCollection dbCollection = template.getCollection(tableName);
List<DBObject> indexInfo = dbCollection.getIndexInfo();
// create index if not exist
if (CollectionUtils.sizeOf(indexInfo) <= 1) {
template.ensureIndex(tableName, "idx_gmtCreated", "gmtCreated");
LOGGER.info("create queue " + tableName);
}
return true;
}
public DBCollection getCollection() {
if(split){
return local.get();
}else{
return coll;
}
}