简体   繁体   中英

MongoDB Query with Java

I have the following database structure in MongoDB (see below) and I want to do a search query on the fields 'name'

Database structure:

         {
            "_id" : ObjectId("505ad9a7ed5022cbe1f3dc2e"),
            "id" : "10",
            "description" : "",  
            "members" : [{
              "id" : "1",
              "name" : "Jack" 
            }, {
              "id" : "2",
              "name" : "Mike" 
            }, {
              "id" : "3",
              "name" : "Laura" 
            }, {
              "id" : "4",
              "name" : "Sara" 
            }, {
              "id" : "240",
              "name" : "Ronald" 
            }],
            "status" : "active"
        },  
        {
            "_id" : ObjectId("5059c214707747cbc5819f6f"),
            "id" : "12",
            "description" : "",  
            "members" : [{
              "id" : "19",
              "name" : "Geoff" 
            }, {
              "id" : "21",
              "name" : "Andrew" 
            }, {
              "id" : "23",
              "name" : "Rachel" 
            }, {
              "id" : "25",
              "name" : "Susan" 
            }],
            "status" : "active"
        },  

I have the following code will do a search on the field 'description' or 'status'. DBCollection collection = database.getCollection("members");

        BasicDBObject or1 = new BasicDBObject(); 
        or1.put("description",  Pattern.compile(keyword, Pattern.CASE_INSENSITIVE)); 

        BasicDBObject or2 = new BasicDBObject(); 
        or2.put("status",  Pattern.compile(keyword, Pattern.CASE_INSENSITIVE));         

        BasicDBList or = new BasicDBList();
        or.add(or1); 
        or.add(or2);        

        BasicDBObject query = new BasicDBObject(); 
        query.put("$or", or);

        BasicDBObject select = new BasicDBObject();
        select.put("description", 1);  
        select.put("status", 1);    
        DBCollection collection = collection.find(query, select);

Question is, how would I do an OR search on the field 'members.name' ? So if I would search for the name or text 'Laura' that the document/record with id 10 will be in the result set.

Would I have to loop through all the results or something like that?

Thanks for any help or suggestions!

If I understand the question correctly, it is really a question about how to search a single field inside of an array. Use the full path to the field in question. In this case, it is "members.name".

Now, if you want to AND the members names together to find records that contain two specific members, just put the individual queries into a BasicDBList as you did with OR and then AND them together. If you want to include the other OR query list, just toss that query object (used in the code sample above) into that BasicDBList to be included with the AND.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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