AlbertEngelB
AlbertEngelB

Reputation: 16456

MongoDB Documentation (Projection vs. Query)

Was checking the MongoDB docs yesterday and noticed a lot of their operators have two entries, one under projection and one under query.

What's the difference between the two? They seem to pretty much cover the same thing.

Upvotes: 4

Views: 1198

Answers (1)

Sammaye
Sammaye

Reputation: 43884

Query is actually querying for records whereas projection is the projection of document fields.

Another way of putting this is to say projection is SELECT in SQL and query is WHERE.

Lets look at an example ( http://docs.mongodb.org/manual/reference/operator/elemMatch/ ):

db.users.find(
    { sessions: {$elemMatch:{session_id: 23}} }, 
    { sessions:{$elemMatch:{session_id: 23}} }
)

This query uses $elemMatch to match a element in the sessions field of a user document while using the very same operator for projection of that found session.

Of course in reality you wouldn't write this query like this since you could just do sessions.$ for the projection part but I wrote it out this way to show you in a more complete manner.

Upvotes: 5

Related Questions