0

I have a File and I want to get the Directory

My mongo table Directory is built this way:

0
   _id
   _class
   listOfFiles[2]
                -0
                      $ref       File
                      $id        idmongodb1   
                -1
                      $ref       File 
                      $id        idmongodb2 
1
   _id
   _class
   listOfFiles[1]
                -0
                      $ref       File
                      $id        idmongodb3   

And I would like to know how to get the way to the idmongodb(1 or 2 or 3)

Can my query :

    Query query = new Query();
    query.addCriteria(
            new Criteria().andOperator(

            Criteria.where("listOfFiles.$id").is(new ObjectId(file.getId()))
            )

    );

    return  mongoTemplate.findOne(query,Directory.class);

work ?

I would like to have confirmations.

In my JavaCode, the corresponding class Directory is buid this way

  public class Directory{

       public List<File> listOfFiles;   

 }

Thanks

2 Answers 2

1

Try $elemMatch (http://docs.mongodb.org/manual/reference/operator/query/elemMatch) for querying elements within a collection.

Sign up to request clarification or add additional context in comments.

Comments

0

The query

 Query query = new Query();
query.addCriteria(
        new Criteria().andOperator(

        Criteria.where("listOfFiles.$id").is(new ObjectId(file.getId()))
        )

);

return  mongoTemplate.findOne(query,Directory.class);

actually works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.