Shariq Ansari
Shariq Ansari

Reputation: 4641

what is the meaning of first {} curly brace in mongodb projection query

I want to know what is the need of first curly brace {} after find here in this query.

db.mycol.find({},{"title":1,_id:0})

Upvotes: 1

Views: 3354

Answers (4)

Atul Jain
Atul Jain

Reputation: 1055

You should refer this link for better explanation http://docs.mongodb.org/manual/reference/operator/projection/positional/

first of all, projection does not return the first result it finds it tells mongo what to return.

.findOne(query) will LIMIT result to one or find(query).limit(1) will also do the same.

You say you are trying to "get all" your data.

a standard find type query will get your started...

find({mongo:query},{mongo:projection})

but limits results to a cursor from the mongo shell*

Upvotes: 0

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20703

It is an empty query, in the sense of a limiting boundary. So

db.mycol.find({},{"title":1,_id:0})

would basically translate to

Show me the title, but not the _id (as you would do by default) for all documents of the mycol collection in the current database.

Let's say you want all the titles written by Alan Turing. So you could modify the query part like this:

db.mycol.find({"author":"Alan Turing"},{"title":1,_id:0})

In general, MongoDB's find operation can be described like this

db.collection.find(query,projection)

For more detailed information, you might want to read find's documentation.

Upvotes: 6

Harpreet Singh
Harpreet Singh

Reputation: 2671

The first Curly braces is used as a where condition in MySql

Check out this Link - SQL to MongoDB Mapping Chart

MySQL

SELECT user_id, status FROM users WHERE status = "A"

MongoDB

db.users.find(
    { status: "A" },
    { user_id: 1, status: 1, _id: 0 }
)

Upvotes: 3

Salvador Dali
Salvador Dali

Reputation: 222761

This is called projection and tells which fields to show. For example here you show only title. It is something like Select title from mycol; If you will not specify projection it will return everything, which is close to select * from mycol;

_id is always shown, unless you hide it explicitly with _id: 0. Another thing is that you can not mix 0 and 1 (except for _id). But one more time - read FAQ.

Upvotes: 0

Related Questions