从 mongo 数据库(Spring 数据)return 子文档的最佳方法是什么

What is the best way to return the subdocument from mongo db (Spring data)

对于名为 "test" 的集合:

{ "_id" : ObjectId("5848604af8ea274676b6asd8"), "test2" : { "_id" : null, "name" : "test3" } }

有没有办法直接让 mongo return 类型 "test2" 的子文档而不是父文档? (使用 spring-数据-mongo)

是的,您可以使用 distinct 投影找到相同的结果。 如果你的 collection 名字是 test 那么-

敬请运行-

db.test.distinct("test2")