在MongoDB中,Java驱动程序可以使用以下代码来获取最后插入的文档的ID:
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
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 MongoDB {
public static void main(String[] args) {
String connectionString = "mongodb+srv://username:password@cluster0.mongodb.net/test?retryWrites=true&w=majority";
MongoClient mongoClient = new MongoClient(new MongoClientURI(connectionString));
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> documents = database.getCollection("test");
Bson match = Aggregates.match(eq("status", "active"));
Bson unwind = Aggregates.unwind("$document");
Bson graphLookup = Aggregates.graphLookup(
"documents",
new Document("$document"),
"author",
"_id",
"graph",
new GraphLookup.GraphLookupOptions().depthField("level"));
Bson addFields = Aggregates.addFields(new Document("graph",
new Document("$concatArrays", Arrays.asList("$graph", "$lookup"))));
Bson unwindGraph = Aggregates.unwind("$graph");
Bson addEdges = Aggregates.addFields(new Document("edges",
new Document("$concatArrays", Arrays.asList("$edges", "$lookup"))));
Bson unwindEdges = Aggregates.unwind("$edges");
Bson addFieldsEdges = Aggregates.addFields(new Document("node", "$node")
.append("edges", new Document("$concatArrays", Arrays.asList("$edges", "$lookup"))));
Bson unwindNode = Aggregates.unwind("$node");
Bson addFieldsNode = Aggregates.addFields(new Document("name", "$name")
.append("age", "$age"));
Bson unwindName = Aggregates.unwind("$name");
Bson addFieldsName = Aggregates.addFields(new Document("full_name", "$full_name"));
Bson unwindAge = Aggregates.unwind("$age");
Bson addFieldsAge = Aggregates.addFields(new Document("age", "$age"));
Bson addDocumentId = Aggregates.addFields(new Document("_id", "$document_id"));
Bson unwindDocumentId = Aggregates.unwind("$document_id");
Bson addFieldsDocumentId = Aggregates.addFields(new Document("document_id", "$document_id"));
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
Bson unwindTimestamp = Aggregates.unwind("$timestamp");
Bson addFieldsTimestamp = Aggregates.addFields(
领取专属 10元无门槛券
手把手带您无忧上云