简体   繁体   English

Java中的MongoDB查询,在嵌套对象中搜索/查找

[英]MongoDB Query in Java, search/find in nested object

I'm having a little trouble with a query in Java on a MongoDB. 我在MongoDB上使用Java查询时遇到了一些麻烦。

I have the follow structure in the database: 我在数据库中有以下结构:

            {
              "_id" : ObjectId("5059c214707747cbc5819f6f"),
              "id" : "7",
              "title" : "test4",
              "keywords" : "keyword 1, keyword 2",

              "partner" : {
                "id" : "1",
                "name" : "partner",
                "keywords" : "partner keyword 1, partner keyword 2"
              },
              "status" : {
                "id" : "0",
                "name" : "Expired"
              },

              "modified" : "2012-09-27"
            }

I want the query the database for the field 'Status.name', example SELECT * FROM table WHERE status.name = 'Expired' 我想查询字段'Sta​​tus.name'的数据库,例如SELECT * FROM table WHERE status.name ='Expired'

How would I do such a query in Java for MongoDB? 我如何用Java为MongoDB做这样的查询?

Thanks for any help or suggestions! 感谢您的任何帮助或建议!

Here is an example: 这是一个例子:

import com.mongodb.Mongo;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DB;

public class MongoTest {

    public static void main(String[] args) throws Exception {

        // connect to the local database server
        Mongo m = new Mongo();

        DB db = m.getDB( "test" );

        DBCollection coll = db.getCollection("test");

        // delete all the data from the 'test' collection
        coll.drop();

        // make a document
        BasicDBObject doc = new BasicDBObject();

        doc.put("id", 7);
        doc.put("title", "test4");
        doc.put("modified", "2012-09-27");

        BasicDBObject status = new BasicDBObject();

        status.put("id", "1");
        status.put("name", "Expired");

        doc.put("status", status);

        // insert
        coll.insert(doc);

        BasicDBObject query = new BasicDBObject("status.name", "Expired");

        //  run the query and print the results out
        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }

        m.close();
    }
}

MongoDB uses a 'match object' as a query. MongoDB使用“匹配对象”作为查询。 So to find objects that have status.name equal to "Expired", you could feed an object like such: 因此,要查找status.name等于“Expired”的对象,您可以提供如下对象:

{ "status.name": "Expired" }

From Java you'll need to create a DBOjbect like the above to use as the match (returning everything that ... matches with the match object) and send that to the database as a query. 从Java开始,您需要创建一个类似上面的DBOjbect作为匹配(返回与匹配对象匹配的所有内容)并将其作为查询发送到数据库。 Assuming you'll have a connection to MongoDB from Java, use the find -method to execute the query you're after. 假设您将从Java连接到MongoDB,请使用find -method执行您之后的查询。

BasicDBObject query = new BasicDBObject();

List<BasicDBObject> ob = new ArrayList<BasicDBObject>();

ob.add(new BasicDBObject("status.name", "Expired"));

List<BasicDBObject> foundDocument =   table.find(query)into(new ArrayList<BasicDBObject>());

for (int i = 0; i < foundDocument.size(); i++) {

System.out.println(foundDocument.get(i));
}

This will print all the rows having status as "Expired" 这将打印状态为“已过期”的所有行

With MongoDB Java Driver v3.2.2 you can do something like this: 使用MongoDB Java Driver v3.2.2,您可以执行以下操作:

FindIterable<Document> iterable = collection.find(Document.parse("{\"status.name\": \"Expired\"}"));

This returns all documents containing "Expired" in the status.name nested field. 这将返回status.name嵌套字段中包含“Expired”的所有文档。

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

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