MongoDB Query in Java, search/find in nested object

前端 未结 4 1878
猫巷女王i
猫巷女王i 2021-01-19 13:16

I\'m having a little trouble with a query in Java on a MongoDB.

I have the follow structure in the database:

            {
              \"_id\" : Ob         


        
相关标签:
4条回答
  • 2021-01-19 13:32
    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"

    0 讨论(0)
  • 2021-01-19 13:41

    MongoDB uses a 'match object' as a query. So to find objects that have status.name equal to "Expired", you could feed an object like such:

    { "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. Assuming you'll have a connection to MongoDB from Java, use the find-method to execute the query you're after.

    0 讨论(0)
  • 2021-01-19 13:42

    With MongoDB Java Driver v3.2.2 you can do something like this:

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

    This returns all documents containing "Expired" in the status.name nested field.

    0 讨论(0)
  • 2021-01-19 13:47

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题