首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

获取带有Java驱动程序的mongoDB中最后插入的文档的ID

在MongoDB中,Java驱动程序可以使用以下代码来获取最后插入的文档的ID:

代码语言:java
复制

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 {

代码语言:txt
复制
public static void main(String[] args) {
代码语言:txt
复制
    String connectionString = "mongodb+srv://username:password@cluster0.mongodb.net/test?retryWrites=true&w=majority";
代码语言:txt
复制
    MongoClient mongoClient = new MongoClient(new MongoClientURI(connectionString));
代码语言:txt
复制
    MongoDatabase database = mongoClient.getDatabase("test");
代码语言:txt
复制
    MongoCollection<Document> documents = database.getCollection("test");
代码语言:txt
复制
    Bson match = Aggregates.match(eq("status", "active"));
代码语言:txt
复制
    Bson unwind = Aggregates.unwind("$document");
代码语言:txt
复制
    Bson graphLookup = Aggregates.graphLookup(
代码语言:txt
复制
            "documents",
代码语言:txt
复制
            new Document("$document"),
代码语言:txt
复制
            "author",
代码语言:txt
复制
            "_id",
代码语言:txt
复制
            "graph",
代码语言:txt
复制
            new GraphLookup.GraphLookupOptions().depthField("level"));
代码语言:txt
复制
    Bson addFields = Aggregates.addFields(new Document("graph",
代码语言:txt
复制
            new Document("$concatArrays", Arrays.asList("$graph", "$lookup"))));
代码语言:txt
复制
    Bson unwindGraph = Aggregates.unwind("$graph");
代码语言:txt
复制
    Bson addEdges = Aggregates.addFields(new Document("edges",
代码语言:txt
复制
            new Document("$concatArrays", Arrays.asList("$edges", "$lookup"))));
代码语言:txt
复制
    Bson unwindEdges = Aggregates.unwind("$edges");
代码语言:txt
复制
    Bson addFieldsEdges = Aggregates.addFields(new Document("node", "$node")
代码语言:txt
复制
            .append("edges", new Document("$concatArrays", Arrays.asList("$edges", "$lookup"))));
代码语言:txt
复制
    Bson unwindNode = Aggregates.unwind("$node");
代码语言:txt
复制
    Bson addFieldsNode = Aggregates.addFields(new Document("name", "$name")
代码语言:txt
复制
            .append("age", "$age"));
代码语言:txt
复制
    Bson unwindName = Aggregates.unwind("$name");
代码语言:txt
复制
    Bson addFieldsName = Aggregates.addFields(new Document("full_name", "$full_name"));
代码语言:txt
复制
    Bson unwindAge = Aggregates.unwind("$age");
代码语言:txt
复制
    Bson addFieldsAge = Aggregates.addFields(new Document("age", "$age"));
代码语言:txt
复制
    Bson addDocumentId = Aggregates.addFields(new Document("_id", "$document_id"));
代码语言:txt
复制
    Bson unwindDocumentId = Aggregates.unwind("$document_id");
代码语言:txt
复制
    Bson addFieldsDocumentId = Aggregates.addFields(new Document("document_id", "$document_id"));
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(new Document("timestamp", "$timestamp"));
代码语言:txt
复制
    Bson unwindTimestamp = Aggregates.unwind("$timestamp");
代码语言:txt
复制
    Bson addFieldsTimestamp = Aggregates.addFields(
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券