简体   繁体   中英

Mongo DB Query in Arraylist

In my Java Play framework application, I want to store the ArrayList values in mongoDB.

{
    "_id" : ObjectId("5832f29bd4c6721e4e8ba4a7"),
    "_class" : "com.netas.innovation.entity.Idea",
    "title" : "fsaf",
    "desc" : "adgg",
    "keyWords" : "dgds",
    "createdDate" : ISODate("2016-11-21T13:11:55.823Z"),
    "checkbox1" : false,
    "checkbox2" : false,
    "checkbox3" : false,
    "scopeOfIdea" : "Herkes",
    "template" : false,
    "creatorUser" : {
        "$ref" : "user",
        "$id" : ObjectId("5832f27dd4c6721e4e8ba4a5")
    },
    "owners" : [ 
        {
            "$ref" : "user",
            "$id" : ObjectId("5832f27dd4c6721e4e8ba4a5")
        }
    ],
    "answer" : {
        "$ref" : "answer",
        "$id" : ObjectId("5832f29bd4c6721e4e8ba4a6")
    },
    "fileList" : []
}

i want to search in owners.

My query doesnt work

if(owners != null && !owners.isEmpty()) {

           for(int i=0; i<owners.size(); i++) {
              criteriaList.add(new **Criteria().elemMatch(Criteria.where("owners.$id").is(owners.get(i).getId())));**
            }
        }

How can i fix? i can search by owners. owners can be two people or three

Thanks for answers

"owners" : [ 
    {
        "$ref" : "user(list)",
        "$id" : ObjectId("5832ecdb0deb78cc88392c83")
    }

$ref : ozgurk,volkany ...

http://prntscr.com/ddz3c9此示例输出... title desc vs vsdate和owner

I'm not a mongodb expert but, I guess you should do somethink like:

List<Criteria> criteriaList = new ArrayList<Criteria>();
...
List idList = new ArrayList();
for (int i = 0; i < owners.size(); i++) {
     idList.add(owners.get(i).getId());
}
criteriaList.add(new Criteria().where("owners.$id").in(idList));
...

If you add each owner=owners.get(i).getId() criteria to the list one by one and finally combine all criterias with AND operation you will not get your desired output.

I guess you use Spring data. I tried to write following mongodb query:

db.getCollection('idea').find(
    {
           "owners.$id": {
               "$in" : [
                     ObjectId("58451c5f13c97bdde9950641"),
                     ObjectId("28451c5f13c97bdde9950642")
               ]
           }
    }
)

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