How to get array of document using Mongodb java?

左心房为你撑大大i 提交于 2019-12-25 02:57:24

问题


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))

回答1:


You have two options:

  • using .find() with cherry-picking which document you have to have fetched.
  • using the aggregation framework by projecting the documents.

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 } } )

which will fetch only the 192_168_10_17 document data.

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




回答2:


By using mongo java driver you can do this by following query -

eventlist.find(new BasicDBObject(), new BasicDBObject("198_168_1_134", 1)) 


来源:https://stackoverflow.com/questions/29771457/how-to-get-array-of-document-using-mongodb-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!