How to get array of document using Mongodb java?

2019-08-20 02:13发布

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.

enter image description here

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

2条回答
疯言疯语
2楼-- · 2019-08-20 02:56

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

eventlist.find(new BasicDBObject(), new BasicDBObject("198_168_1_134", 1)) 
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-20 03:01

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.

查看更多
登录 后发表回答