1

I have a collection of emails in MongoDB with a field containing an array of json.

How can knowing the sender and the receiver, how can I find all the emails exchanged between the two people ? I need to do something like

db.email.find({"from": [email protected]", "tos":"[email protected]")

but I cant find the right way to write this query :(

> db.emails.findOne() { "from" : { "real_name" : "it", "address" : "[email protected]" }, "tos" : [ { "real_name" : null, "address" : "[email protected]" } ], } 

3 Answers 3

4

Use "from.address" and "tos.address":

db.emails.find({"from.address" : "[email protected]", "tos.address" : "[email protected]"}) 
Sign up to request clarification or add additional context in comments.

1 Comment

3

Each field is considered as a json, we can precise the expected value through a "." :

db.emails.find({"tos.address" : "[email protected]", "from.address":"[email protected]"}) 

Comments

1

From and To are objects and data inside them can be accessed through a . operator. So the query will be:

db.emails.find({"from.address" : "[email protected]", "tos.address" : "[email protected]"}). 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.