繁体   English   中英

Mongodb java驱动3.0查询

[英]Mongodb java driver 3.0 query

您好我正在使用mongodb java驱动程序3.0。 我有一个文件,如:

db.employee.find().pretty()

   "_id" : ObjectId("559e4ae4aed7561c94e565d5"),
   "empid" : 1,
   "name" : "abc",
   "dob" : "17/05/1990",
   "address" : [
           "559e4ae3aed7561c94e565d3",
           "559e4ae4aed7561c94e565d4"
   ]

我想查询地址字段并获取所有地址(在我的情况下是上面的那些值),而不指定哪个值。 我的代码是:

FindIterable<Document> iterable = mongoDatabase.getCollection("employee").find(
        new Document("address", 559e4ae3aed7561c94e565d3));
iterable.forEach(new Block<Document>() {
    @Override
    public void apply(final Document document) 
        System.out.println(document);
    }
});  

我不想手动指定地址。 如果我查询地址,我应该得到这两个值。 有什么建议?

得到了答案,我们可以查询其他已知字段,例如:empid而不是返回整个文档,只返回您需要的字段(在我的案例地址中)。

FindIterable<Document> iterable = mongoDatabase.getCollection("employee").find(
            new Document("empid", 1));
    iterable.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            System.out.println(document.get("address").toString());
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM