简体   繁体   English

如何在MongoDB Java驱动程序中表示具有多个字段的对象

[英]How to Represent Objects with Multiple Fields in the MongoDB Java Driver

I'm creating an aggregation pipeline in Java using MongoDB and, of course, the MongoDB Java driver. 我正在使用MongoDB(当然还有MongoDB Java驱动程序)在Java中创建一个聚合管道。

I've figured out how to do what I want in the Mongo Shell, but I'm not sure how to translate this into Java, particularly the whole concept of an object with commas in it, as shown below. 我已经找到了如何在Mongo Shell中做我想做的事,但是我不确定如何将其转换为Java,尤其是其中包含逗号的对象的整个概念,如下所示。

{$project: {sleep: {"date": 1, "hours": 1}}}

Any thoughts on the best way to do this? 关于最佳方法的任何想法?

Turns out one great way to do this is the following: 事实证明,执行此操作的一种好方法如下:

BasicDBObject project = new BasicDBObject("$project", new BasicDBObject("sleep", new BasicDBObject().append("date", 1).append("hours", 1)));

Then you can call your aggregation command as follows: 然后,您可以按以下方式调用聚合命令:

AggregationOutput output = collection.aggregate(commandOne, commandTwo, Project, CommandThree);

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM