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

时间:2021-12-31 17:16:55
DB db = mongo.getDB("sample");
DBCollection table = db.getCollection("samplecollection");
DBCursor cursor2 = table.find();

gives the result as: { "_id" : { "$oid" : "58bfbcff1d30d8a8c1194328"} , "ID" : 1 , "NAME" : "dgsdg"}

结果为:{“_ id”:{“$ oid”:“58bfbcff1d30d8a8c1194328”},“ID”:1,“NAME”:“dgsdg”}

How to get the document without objectid?

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

4 个解决方案

#1


3  

You can use projection for that.

你可以使用投影。

Try this:

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

Read this MongoDB documentation to know more.

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

#2


0  

I have done this and worked:

我做了这个并且工作了:

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);
       }        

#3


0  

First you will be import this, import com.mongodb.client.model.Projections;

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

Then again, u try this one also..

再说一遍,你也试试这个......

MongoClient client = new MongoClient("hostName");
MongoDatabase db = client.getDatabase("dbName");
MongoCollection<Document> collection = db.getCollection("collectionName");
collection.withWriteConcern(new WriteConcern(1));
/*   whatever field you dosn`t need.. you will run this following code, 

        FindIterable<Document> findDoc = collection.find().projection(new Document().append("_id",0));  //if you need mean, you put _id value is 1.

    */
FindIterable<Document> findDoc = collection.find().projection(Projections.excludeId()) ;
 for (Document doc : findDoc) {
       System.out.println(doc);
   }        

#4


-1  

You can use the aggregation framework to achieve what you want:

您可以使用聚合框架来实现您的目标:

db.getCollection('samplecollection').aggregate([{$project: { field1:1 ,field2: 1.....,'_id': 0} }])

db.getCollection('samplecollection')。aggregate([{$ project:{field1:1,field2:1 .....,'_ id':0}}])

https://docs.mongodb.com/manual/reference/operator/aggregation/project/

#1


3  

You can use projection for that.

你可以使用投影。

Try this:

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

Read this MongoDB documentation to know more.

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

#2


0  

I have done this and worked:

我做了这个并且工作了:

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);
       }        

#3


0  

First you will be import this, import com.mongodb.client.model.Projections;

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

Then again, u try this one also..

再说一遍,你也试试这个......

MongoClient client = new MongoClient("hostName");
MongoDatabase db = client.getDatabase("dbName");
MongoCollection<Document> collection = db.getCollection("collectionName");
collection.withWriteConcern(new WriteConcern(1));
/*   whatever field you dosn`t need.. you will run this following code, 

        FindIterable<Document> findDoc = collection.find().projection(new Document().append("_id",0));  //if you need mean, you put _id value is 1.

    */
FindIterable<Document> findDoc = collection.find().projection(Projections.excludeId()) ;
 for (Document doc : findDoc) {
       System.out.println(doc);
   }        

#4


-1  

You can use the aggregation framework to achieve what you want:

您可以使用聚合框架来实现您的目标:

db.getCollection('samplecollection').aggregate([{$project: { field1:1 ,field2: 1.....,'_id': 0} }])

db.getCollection('samplecollection')。aggregate([{$ project:{field1:1,field2:1 .....,'_ id':0}}])

https://docs.mongodb.com/manual/reference/operator/aggregation/project/