Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在MongoDB中返回没有objectId的集合文档?_Java_Mongodb_Mongodb Java - Fatal编程技术网

Java 如何在MongoDB中返回没有objectId的集合文档?

Java 如何在MongoDB中返回没有objectId的集合文档?,java,mongodb,mongodb-java,Java,Mongodb,Mongodb Java,结果如下所示: {“_id”:{“$oid”:“58bfbcff1d30d8a8c1194328”},id:1,NAME:“dgsdg”} DBCursor cursor2 = table.find().projection(excludeId()) 如何在没有objectid的情况下获取文档?您可以使用projection DBCursor cursor2 = table.find().projection(excludeId()) 试试这个: DB db = mongo.getDB(

结果如下所示: {“_id”:{“$oid”:“58bfbcff1d30d8a8c1194328”},id:1,NAME:“dgsdg”}

DBCursor cursor2 = table.find().projection(excludeId()) 

如何在没有objectid的情况下获取文档?

您可以使用
projection

DBCursor cursor2 = table.find().projection(excludeId()) 
试试这个:

DB db = mongo.getDB("sample");
DBCollection table = db.getCollection("samplecollection");
DBCursor cursor2 = table.find();
DBCursor cursor2 = table.find().projection(excludeId()) 

阅读MongoDB文档了解更多信息。

您可以使用聚合框架实现您想要的:

DBCursor cursor2 = table.find().projection(excludeId()) 
db.getCollection('samplecollection').aggregate([{$project:{field1:1,field2:1….,''u id':0}}]))

DBCursor cursor2 = table.find().projection(excludeId()) 

我已经完成了这项工作:

DBCursor cursor2 = table.find().projection(excludeId()) 
MongoDatabase=mongo.getDatabase(“db”);
MongoCollection contCol=database.getCollection(“测试”);
FindTable it=contCol.find().projection(excludeId());
ArrayList docs=新建ArrayList();
for(org.bson.Document-its:it){
System.out.println(its);
}        

首先,您将导入这个,导入com.mongodb.client.model.Projections

DBCursor cursor2 = table.find().projection(excludeId()) 
再说一次,你也试试这个

DBCursor cursor2 = table.find().projection(excludeId()) 
MongoDatabase database = mongo.getDatabase("db");
     MongoCollection<org.bson.Document> contCol = database.getCollection("test");
     FindIterable<org.bson.Document> it = contCol.find().projection(excludeId());
     ArrayList<Document> docs = new ArrayList();


       for (org.bson.Document its : it) {
           System.out.println(its);
       }        
MongoClient客户端=新的MongoClient(“主机名”);
MongoDatabase db=client.getDatabase(“dbName”);
MongoCollection collection=db.getCollection(“collectionName”);
集合。使用WriteConcern(新WriteConcern(1));
/*你不需要的任何领域。。您将运行以下代码,
FindTerable findDoc=collection.find().projection(新文档().append(“_id”,0))//如果你需要平均值,你把_id的值设为1。
*/
FindTerable findDoc=collection.find().projection(Projections.excludeId());
用于(文件文档:findDoc){
系统输出打印项次(doc);
}        

对于旧版本的java客户端不确定,但使用3.2+时,如果您在数据库中定义了一个文档,您可以从数据库中获取该文档,但该文档不包含Object。当我这样做时,它可能会重复显示错误:类型不匹配:无法从Object[]转换为dbcursorry,有一些错误,我已经更新了答案和链接,请查看错误“MongoData类型的excludeId()方法未定义”是否有任何方法可以用java获取上述文档的字段类型?您是指文档中所有字段的字段类型?