Java MongoDB 查询

本文介绍如何从集合中查询文档的通用方法。

测试数据

插入5条测试文档

{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"}
{ "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"}
{ "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"}
{ "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"}
{ "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}

1. find()

1.1 获取第一条文档

Document document = collection.find().first();
System.out.println(document);

输出:

{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"}

1.2 获取所有文档

FindIterable<Document> documents = collection.find();
MongoCursor<Document> mongoCursor = documents.iterator();
while (mongoCursor.hasNext()) {
    System.out.println(mongoCursor.next());
}

输出:

{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"}
{ "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"}
{ "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"}
{ "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"}
{ "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}

1.3 获取文档的单一字段

Document fields = new Document();
fields.put("name", 1);

FindIterable<Document> projection = collection.find().projection(fields);
MongoCursor<Document> iterator = projection.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

输出:

{ "_id" : { "$oid" : "id"} , "name" : "mkyong-1"}
{ "_id" : { "$oid" : "id"} , "name" : "mkyong-2"}
{ "_id" : { "$oid" : "id"} , "name" : "mkyong-3"}
{ "_id" : { "$oid" : "id"} , "name" : "mkyong-4"}
{ "_id" : { "$oid" : "id"} , "name" : "mkyong-5"}

2. 使用find()对比查询

2.1 获取所有number = 5的文档

Document whereQuery = new Document();
whereQuery.put("number", 5);
FindIterable<Document> whereDocuments = collection.find(whereQuery);
MongoCursor<Document> whereIterator = whereDocuments.iterator();
while (whereIterator.hasNext()) {
    System.out.println(whereIterator.next());
}

输出:

{ "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}

2.2 $in - 获取number在2、4、5中的文档

Document inQuery = new Document();
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(4);
list.add(5);
inQuery.put("number", new Document("$in", list));
FindIterable<Document> listDocuments = collection.find(inQuery);
MongoCursor<Document> listIterator = listDocuments.iterator();
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}

输出:

{ "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"}
{ "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"}
{ "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}

2.3 $gt $lt - 获取5 > number > 2的文档

Document gtQuery = new Document();
gtQuery.put("number", new Document("$gt", 2).append("$lt", 5));
FindIterable<Document> gtDocuments = collection.find(gtQuery);
MongoCursor<Document> gtIterator = gtDocuments.iterator();
while (gtIterator.hasNext()) {
    System.out.println(gtIterator.next());
}

输出:

{ "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"}
{ "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"}

2.4 $ne - 获取number != 4的文档

Document neQuery = new Document();
neQuery.put("number", new Document("$ne", 4));
FindIterable<Document> neDocuments = collection.find(neQuery);
MongoCursor<Document> neIterator = neDocuments.iterator();
while (neIterator.hasNext()) {
    System.out.println(neIterator.next());
}

输出:

{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"}
{ "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"}
{ "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"}
{ "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}

3. 使用find()进行逻辑查询

3.1 $and - 获取number = 2 and name = 'mkyong-2'的文档

Document andQuery = new Document();

List<Document> obj = new ArrayList<Document>();
obj.add(new Document("number", 2));
obj.add(new Document("name", "mkyong-2"));
andQuery.put("$and", obj);

System.out.println(andQuery.toString());

FindIterable<Document> andDocuments = collection.find(andQuery);
MongoCursor<Document> andIterator = andDocuments.iterator();
while (andIterator.hasNext()) {
    System.out.println(andIterator.next());
}

输出:

{ "$and" : [ { "number" : 2} , { "name" : "mkyong-2"}]}

{ "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"}

4. 使用find()通过正则表达式查询

4.1 $regex

Document regexQuery = new Document();
regexQuery.put("name",
        new Document("$regex", "Mky.*-[1-3]")
                .append("$options", "i"));

System.out.println(regexQuery.toString());

FindIterable<Document> regexDocuments = collection.find(regexQuery);
MongoCursor<Document> regexIterator = regexDocuments.iterator();
while (regexIterator.hasNext()) {
    System.out.println(regexIterator.next());
}

输出:

{ "name" : { "$regex" : "Mky.*-[1-3]" , "$options" : "i"}}

{ "_id" : { "$oid" : "515ad59e3004c89329c7b259"} , "number" : 1 , "name" : "mkyong-1"}
{ "_id" : { "$oid" : "515ad59e3004c89329c7b25a"} , "number" : 2 , "name" : "mkyong-2"}
{ "_id" : { "$oid" : "515ad59e3004c89329c7b25b"} , "number" : 3 , "name" : "mkyong-3"}

5. 完整实例

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class FindDocument {
    public static void insertDummyDocuments(MongoCollection<Document> collection) {

        List<Document> list = new ArrayList<Document>();

        Calendar cal = Calendar.getInstance();

        for (int i = 1; i <= 5; i++) {

            Document data = new Document();
            data.append("number", i);
            data.append("name", "mkyong-" + i);
            // data.append("date", cal.getTime());

            // +1 day
            cal.add(Calendar.DATE, 1);

            list.add(data);

        }

        collection.insertMany(list);

    }

    public static void main(String[] args) {

        try {

            MongoClient mongoClient = new MongoClient("localhost", 27017);
            MongoDatabase database = mongoClient.getDatabase("test");

            // get a single collection
            MongoCollection<Document> collection = database.getCollection("dummyColl");

            insertDummyDocuments(collection);

            System.out.println("1. Find first matched document");
            Document document = collection.find().first();
            System.out.println(document);

            System.out.println("\n1. Find all matched documents");
            FindIterable<Document> documents = collection.find();
            MongoCursor<Document> mongoCursor = documents.iterator();
            while (mongoCursor.hasNext()) {
                System.out.println(mongoCursor.next());
            }

            System.out.println("\n1. Get 'name' field only");
//            Document allQuery = new Document();
            Document fields = new Document();
            fields.put("name", 1);

            FindIterable<Document> projection = collection.find().projection(fields);
            MongoCursor<Document> iterator = projection.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }

            System.out.println("\n2. Find where number = 5");
            Document whereQuery = new Document();
            whereQuery.put("number", 5);
            FindIterable<Document> whereDocuments = collection.find(whereQuery);
            MongoCursor<Document> whereIterator = whereDocuments.iterator();
            while (whereIterator.hasNext()) {
                System.out.println(whereIterator.next());
            }

            System.out.println("\n2. Find where number in 2,4 and 5");
            Document inQuery = new Document();
            List<Integer> list = new ArrayList<Integer>();
            list.add(2);
            list.add(4);
            list.add(5);
            inQuery.put("number", new Document("$in", list));
            FindIterable<Document> listDocuments = collection.find(inQuery);
            MongoCursor<Document> listIterator = listDocuments.iterator();
            while (listIterator.hasNext()) {
                System.out.println(listIterator.next());
            }

            System.out.println("\n2. Find where 5 > number > 2");
            Document gtQuery = new Document();
            gtQuery.put("number", new Document("$gt", 2).append("$lt", 5));
            FindIterable<Document> gtDocuments = collection.find(gtQuery);
            MongoCursor<Document> gtIterator = gtDocuments.iterator();
            while (gtIterator.hasNext()) {
                System.out.println(gtIterator.next());
            }

            System.out.println("\n2. Find where number != 4");
            Document neQuery = new Document();
            neQuery.put("number", new Document("$ne", 4));
            FindIterable<Document> neDocuments = collection.find(neQuery);
            MongoCursor<Document> neIterator = neDocuments.iterator();
            while (neIterator.hasNext()) {
                System.out.println(neIterator.next());
            }

            System.out.println("\n3. Find when number = 2 and name = 'mkyong-2' example");
            Document andQuery = new Document();

            List<Document> obj = new ArrayList<Document>();
            obj.add(new Document("number", 2));
            obj.add(new Document("name", "mkyong-2"));
            andQuery.put("$and", obj);

            System.out.println(andQuery.toString());

            FindIterable<Document> andDocuments = collection.find(andQuery);
            MongoCursor<Document> andIterator = andDocuments.iterator();
            while (andIterator.hasNext()) {
                System.out.println(andIterator.next());
            }

            System.out.println("\n4. Find where name = 'Mky.*-[1-3]', case sensitive example");
            Document regexQuery = new Document();
            regexQuery.put("name",
                    new Document("$regex", "Mky.*-[1-3]")
                            .append("$options", "i"));

            System.out.println(regexQuery.toString());

            FindIterable<Document> regexDocuments = collection.find(regexQuery);
            MongoCursor<Document> regexIterator = regexDocuments.iterator();
            while (regexIterator.hasNext()) {
                System.out.println(regexIterator.next());
            }

            collection.drop();

            System.out.println("Done");

        } catch (MongoException e) {
            e.printStackTrace();
        }

    }
}
对于提升 JavaMongoDB 查询速度的方法,有以下几点建议: 1. 创建适当的索引:在 MongoDB 中,索引可以显著提高查询性能。确保在查询经常使用的字段上创建索引,特别是在经常进行排序、过滤或分组的字段上。可以使用 MongoDB 的 `createIndex` 方法来创建索引。 2. 使用投影(Projection)减少返回数据量:在进行查询时,使用投影操作符来指定只返回所需的字段,以减少数据传输量。这可以通过将需要返回的字段设置为 1 或排除不需要返回的字段(设置为 0)来实现。 3. 使用聚合管道(Aggregation Pipeline):聚合管道是 MongoDB 中用于处理数据的强大工具。通过使用聚合操作符(如 `$match`、`$group`、`$project` 等),可以对数据进行多个操作,并将结果返回给客户端。使用聚合管道可以减少数据库往返次数,提高查询效率。 4. 优化查询语句:确保查询语句使用正确的查询条件和运算符,以便 MongoDB 可以有效地选择索引并执行查询。可以使用 MongoDB 的 `explain()` 方法来分析查询执行计划,并根据结果进行优化。 5. 批量操作:如果需要执行多个查询或更新操作,尽量使用批量操作(如 `insertMany`、`updateMany` 等)而不是逐个操作。这样可以减少与数据库的通信次数,提高效率。 6. 考虑数据模型设计:合理设计数据模型可以提高查询性能。根据应用的查询需求和数据访问模式,合理划分和组织文档结构,以减少查询时的数据扫描和连接操作。 7. 考虑使用缓存:如果查询结果较为稳定且数据量较大,可以考虑使用缓存技术,将查询结果缓存到内存中,以减少对数据库的频繁访问。 以上是一些常见的方法和技巧,可以帮助提升 JavaMongoDB查询速度。根据具体的应用场景和需求,还可以进一步深入优化和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值