Java 从mongodb集合中的数组获取值 收藏: { “用户名”:”gorim99949@x1post.com", “频道”:[ { “频道名称”:“yyyy”, “id”:383, “url”:”https://xxxxxxxxx", } ], } @文档(collection=“user\u masters”) 公共类用户管理员{ @字段(“用户名”) 私有字符串用户名; @DBRef 私有列表用户通道用户通道; ******接二连三********* @文档(collection=“channels”) 公共类用户通道{ 私有字符串url; ************吸气剂/塞特**************** 存储库类 公共UserMasters findByUserName(字符串用户名); @试验方法 查询:userRepository.findByUserName(userName.getUserChannels().getUrl();

Java 从mongodb集合中的数组获取值 收藏: { “用户名”:”gorim99949@x1post.com", “频道”:[ { “频道名称”:“yyyy”, “id”:383, “url”:”https://xxxxxxxxx", } ], } @文档(collection=“user\u masters”) 公共类用户管理员{ @字段(“用户名”) 私有字符串用户名; @DBRef 私有列表用户通道用户通道; ******接二连三********* @文档(collection=“channels”) 公共类用户通道{ 私有字符串url; ************吸气剂/塞特**************** 存储库类 公共UserMasters findByUserName(字符串用户名); @试验方法 查询:userRepository.findByUserName(userName.getUserChannels().getUrl();,java,mongodb,mongodb-query,aws-documentdb,Java,Mongodb,Mongodb Query,Aws Documentdb,如何从mongodb集合中的数组中获取url值。我无法读取集合中数组中的值。请提前感谢您的帮助。您需要自定义查询才能执行此操作。 运行此查询: 聚合({$match:{username:gorim99949@x1post.com“},{$project:{”userChannels:“$channels.url”}}); 它将返回: [{“_id”:ObjectId(“5A934E0001020030405000000”),“用户通道”:https://xxxxxxxxx"]}]. 您可以通过在

如何从mongodb集合中的数组中获取url值。我无法读取集合中数组中的值。请提前感谢您的帮助。您需要自定义查询才能执行此操作。 运行此查询: 聚合({$match:{username:gorim99949@x1post.com“},{$project:{”userChannels:“$channels.url”}}); 它将返回: [{“_id”:ObjectId(“5A934E0001020030405000000”),“用户通道”:https://xxxxxxxxx"]}].
您可以通过在投影中将返回文档的_id设置为0来删除它。

您需要一个自定义查询来完成此操作。 运行此查询: 聚合({$match:{username:gorim99949@x1post.com“},{$project:{”userChannels:“$channels.url”}}); 它将返回: [{“_id”:ObjectId(“5A934E0001020030405000000”),“用户通道”:https://xxxxxxxxx"]}].
您可以通过在投影中将其设置为0来从返回文档中删除_id。

userChannels是一个
列表
为什么不尝试(例如)
…getUserChannels().get(0).getUrl()
?userChannels是一个
列表
为什么不尝试(例如)
…getUserChannels().get(0).getUrl()
collection:
{    
    "username" : "gorim99949@x1post.com",
    "channels" : [ 
        {
            "channelName" : "yyyyy",
            "id" : 383,
            "url" : "https://xxxxxxxxx",
        }
    ],
}

@Document(collection = "user_masters")
public class UserMasters {
  @Field("username")
  private String userName;

  @DBRef
  private List<UserChannels> UserChannels userChannels;
   ******getter and setter*********

@Document(collection="channels")
public class UserChannels {
  private String url;
 ************getter/setter****************


Repository class
public UserMasters  findByUserName(String username);

@Test method
Query: userRepository.findByUserName(userName).getUserChannels().getUrl();