[英]MongoDB 3.2 Java Driver: How To Use AggregateOperation
I am interested in example code for using the MongoDB AggregateOperation driver to perform an $unwind activity. 我对使用MongoDB AggregateOperation驱动程序执行$ unwind活动的示例代码感兴趣。 AggregateOperation seems to be associated with the MongoDB version 3+ Java driver.
AggregateOperation似乎与MongoDB版本3+ Java驱动程序相关联。
Using AggregateOperation, how would I unwind the "sizes" array shown below? 使用AggregateOperation,我如何展开下面显示的“ sizes”数组?
{ "_id" : ObjectId("56928aa190f425c1e987abc3d"), "item" : "Fleece-lined jeans", "sizes" : [ "30", "31", "38", "40", "42" ], "count" : 3 }
The aggregation examples I see on this forum seem mostly to use BasicDBObject objects and these apparently do not mix with the version 3 Java API. 我在这个论坛上看到的聚合示例似乎主要使用BasicDBObject对象,并且这些显然与3版Java API混合使用。
You will have to invoke a static method parse
inside the import org.bson.Document;
您将必须在
import org.bson.Document;
调用静态方法parse
import org.bson.Document;
class. 类。
So, the below lines of code, should unwind the sizes
array object for you in mongodb3.0 driver. 因此,以下代码行应在mongodb3.0驱动程序中为您解开
sizes
数组对象。
MongoClient client = new MongoClient();
MongoDatabase mongoDatabase = client.getDatabase("test");
MongoCollection<Document> mongoCollection = mongoDatabase
.getCollection("unwind");
List<Document> pipeline;
pipeline = Arrays.asList(Document.parse("{$unwind:\"$sizes\"}"));
List<Document> results = mongoCollection.aggregate(pipeline).into(
new ArrayList<Document>());
for (Document cur : results) {
System.out.println(cur.toJson());
}
For 3.2 java driver. 对于3.2 Java驱动程序。
import static com.mongodb.client.model.Aggregates.unwind;
Bson unwind = unwind("$sizes");
List<Bson> pipeline = asList(unwind);
MongoClient c = new MongoClient();
MongoCollection<Document> collection = c.getDatabase("test")
.getCollection("unwind");
List<Document> results = collection.aggregate(pipeline).into(
new ArrayList<Document>());
for (Document cur : results) {
System.out.println(cur.toJson());
}
}
Here test
is the database and unwind
is the collection. 这里
test
是数据库, unwind
是集合。 This unwinds the sizes array object and below are the output documents. 这展开了sizes数组对象,下面是输出文档。
{ "_id" : { "$oid" : "5692fed991b81360d23f1b54" }, "item" : "Fleece-lined jeans", "sizes" : "30", "count" : 3.0 }
{ "_id" : { "$oid" : "5692fed991b81360d23f1b54" }, "item" : "Fleece-lined jeans", "sizes" : "31", "count" : 3.0 }
{ "_id" : { "$oid" : "5692fed991b81360d23f1b54" }, "item" : "Fleece-lined jeans", "sizes" : "38", "count" : 3.0 }
{ "_id" : { "$oid" : "5692fed991b81360d23f1b54" }, "item" : "Fleece-lined jeans", "sizes" : "40", "count" : 3.0 }
{ "_id" : { "$oid" : "5692fed991b81360d23f1b54" }, "item" : "Fleece-lined jeans", "sizes" : "42", "count" : 3.0 }
Above code didn't use DBObject class which is deprecated in 3.0 version of MongoDB java driver. 上面的代码未使用在3.0版本的MongoDB Java驱动程序中已弃用的DBObject类。
Have tested the 3.0 version of code in 3.2 version of mongodb driver and it produced the same result. 在mongodb驱动程序的3.2版本中测试了3.0版本的代码,并且产生了相同的结果。
I tried this solution. 我尝试了这种解决方案。 It is based on This documentation
它基于本文档
AggregateIterable<Document> iterable = db.getCollection("inventory").aggregate(asList(
new Document("$unwind", "$sizes")
));
iterable.forEach(new Block<Document>() {
public void apply(Document document) {
System.out.println(document.toJson());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.