pymongo collection.find() 对比 java MongoDatabse.getCollection.find()

pymongo collection.find() vs java MongoDatabse.getCollection.find()

当我将从 MongoDB java 驱动程序函数 MongoDatabase.getCollection().find() 返回的 org.bson.Document 传递给 elasticsearch 索引时,出现以下异常。

MapperParsingException[Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters.]

这是代码,

MongoCursor<Document> cursor = mongoCollection.find().iterator();
Document doc = cursor.next();
IndexRequest indexRequest = new IndexRequest(indexName, indexType);
indexRequest.source(doc.toJson());  
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(indexRequest);
bulkRequest.execute().actionGet();

但是在传递从 MongoClient().mydb.collection_name.find()

返回的 pymongo 文档时没有出现这样的错误

论文API有什么区别? java 相当于 pymongo 的 find() API 是什么?

Python 的 elasticsearch 批量索引 API 在内部提取 "_id" 字段,因为它是一个元数据字段。
当我们使用 Java 客户端时,我们必须将其删除。

MongoCursor<Document> cursor = mongoCollection.find().iterator();
Document doc = cursor.next();
String id = doc.getString("_id"); // get the _id
doc.remove("_id");   // remove the _id field
IndexRequest indexRequest = new IndexRequest(indexName, indexType, id);
indexRequest.source(doc.toJson());  
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(indexRequest);
bulkRequest.execute().actionGet();