0
如何將這個mongo shell查詢翻譯成java?
db.collection.find({"array.element": "value"}, {"array.$": 1, "_id": 0})
如何將這個mongo shell查詢翻譯成java?
db.collection.find({"array.element": "value"}, {"array.$": 1, "_id": 0})
您可以使用Projections.elemMatch(String fieldName)
變體進行位置投影。
喜歡的東西
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.elemMatch;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
List<Document> results = collection.find(eq("array.element", "value")).projection(fields(elemMatch("array"), excludeId())