MongoDB Java驱动程序通过使用MongoDB Java Driver,可以获取MongoDB的状态信息。
以下是获取mongoStat信息的示例代码:
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.GraphLookup;
import com.mongodb.client.model.LookupOptions;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.Arrays;
public class MongoStat {
public static void main(String[] args) {
String connectionString = "mongodb+srv://username:password@cluster0.mongodb.net/test?retryWrites=true&w=majority";
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("test");
Bson match = Aggregates.match(eq("_id", 1));
Bson unwind = Aggregates.unwind("$document");
Bson graphLookup = Aggregates.graphLookup(
"collection1",
new Document("$id"),
"collection1",
new GraphLookup.GraphLookupOptions().depthField("depth"));
Bson addFields = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFields = Aggregates.unwind("$document");
Bson addGraphLookup = Aggregates.addGraphLookup(
"collection2",
new Document("$id"),
"collection2",
new GraphLookup.GraphLookupOptions().depthField("depth"));
Bson addFieldsAddGraphLookup = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsAddGraphLookup = Aggregates.unwind("$document");
Bson graphLookupArray = Aggregates.graphLookup(
"collection1",
new Document("$id"),
"collection1",
new GraphLookup.GraphLookupOptions().depthField("depth"));
Bson addFieldsGraphLookupArray = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArray = Aggregates.unwind("$document");
Bson graphLookupArrayAddFields = Aggregates.graphLookup(
"collection1",
new Document("$id"),
"collection1",
new GraphLookup.GraphLookupOptions().depthField("depth"));
Bson addFieldsGraphLookupArrayAddFields = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArrayAddFields = Aggregates.unwind("$document");
Bson addFieldsGraphLookupArrayAddFieldsArray = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArrayAddFieldsArray = Aggregates.unwind("$document");
Bson addFieldsGraphLookupArrayAddFieldsArrayArray = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArrayAddFieldsArrayArray = Aggregates.unwind("$document");
Bson addFieldsGraphLookupArrayAddFieldsArrayArrayArray = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArrayAddFieldsArrayArrayArray = Aggregates.unwind("$document");
Bson addFieldsGraphLookupArrayAddFieldsArrayArrayArrayArray = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArrayAddFieldsArrayArrayArrayArray = Aggregates.unwind("$document");
Bson addFieldsGraphLookupArrayAddFieldsArrayArrayArrayArrayArray = Aggregates.addFields(new Document("document",
new Document("$concatArrays", Arrays.asList("$document", "$lookup"))));
Bson unwindAddFieldsGraphLookupArrayAddFieldsArrayArrayArrayArrayArray = Aggregates.unwind("$document");
B
领取专属 10元无门槛券
手把手带您无忧上云