如何使用Mongodb Java获取文档数组? [英] How to get array of document using Mongodb java?

查看:404
本文介绍了如何使用Mongodb Java获取文档数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在mongodb java中获取数组下的所有文档.我的数据库如下.要检索数组198_168_1_134下的所有数据.

How to get all the document under array in mongodb java. My Database is as below. Want to retrieve all the data under array 198_168_1_134.

下面是我尝试过的一些内容

below is some of What i tried,

eventlist.find(new BasicDBObject("$match","192_168_10_17"))
eventlist.find(new BasicDBObject("$elemMatch","192_168_10_17"))
eventlist.find(null, new BasicDBObject("$192_168_10_17", 1))

推荐答案

您有两个选择:

  • 使用.find()并挑选要获取的文档.
  • 通过投影文档使用汇总框架.
  • using .find() with cherry-picking which document you have to have fetched.
  • using the aggregation framework by projecting the documents.

通过使用.find(),您可以执行以下操作:

By using .find() , you can do:

db.collection.find({}, { 192_168_10_17 : 1 })

通过使用聚合框架,您可以执行以下操作:

By using the aggregation framework, you can do:

db.collection.aggregate( { $project : { 192_168_10_17 : 1 } } )

将仅获取 192_168_10_17文档数据.

当然,为了使它在Java中运行,您必须将这些查询翻译到相应的BasicDBObject实例链.

Of course, in order to get this working in Java, you have to translate these queries to a corresponding chain of BasicDBObject instances.

这篇关于如何使用Mongodb Java获取文档数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆