Romain Jouin
Romain Jouin

Reputation: 4818

Querying mongodb

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]"
        }
    ],

}

Upvotes: 1

Views: 7916

Answers (3)

alok kumar
alok kumar

Reputation: 71

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]"}).

Upvotes: 1

Romain Jouin
Romain Jouin

Reputation: 4818

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]"})

Upvotes: 3

anhlc
anhlc

Reputation: 14429

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

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

Upvotes: 4

Related Questions