# Query with conditions on multiple documents which have same value for one field

**URL:** <https://discuss.elastic.co/t/query-with-conditions-on-multiple-documents-which-have-same-value-for-one-field/69004>\
**Category:** Elasticsearch\
**Created:** [December 14, 2016, 10:25am UTC](https://discuss.elastic.co/t/query-with-conditions-on-multiple-documents-which-have-same-value-for-one-field/69004 "2016-12-14T10:25:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![boonzz](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@boonzz](https://discuss.elastic.co/u/boonzz)\
**Post date:** [December 14, 2016, 10:25am UTC](https://discuss.elastic.co/t/query-with-conditions-on-multiple-documents-which-have-same-value-for-one-field/69004/1 "2016-12-14T10:25:50Z")

</div>

I have data of this format in elasticsearch, each one is in seperate document:

{ 'pid': 1, 'nm' : 'tom'}, { 'pid': 1, 'nm' : 'dick''},{ 'pid': 1, 'nm' : 'harry'}, { 'pid': 2, 'nm' : 'tom'}, { 'pid': 2, 'nm' : 'harry'}, { 'pid': 3, 'nm' : 'dick'}, { 'pid': 3, 'nm' : 'harry'}, { 'pid': 4, 'nm' : 'harry'}

And I need to find the pid's which have 'harry' and do not have 'tom', which here are 3 and 4. How do I query that?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 11, 2017, 10:26am UTC](https://discuss.elastic.co/t/query-with-conditions-on-multiple-documents-which-have-same-value-for-one-field/69004/2 "2017-01-11T10:26:24Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
