Reputation: 775
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
Upvotes: 0
Views: 86
Reputation: 775
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
Upvotes: 0
Reputation: 18163
Try $elemMatch
(http://docs.mongodb.org/manual/reference/operator/query/elemMatch) for querying elements within a collection.
Upvotes: 1