Ionică Bizău
Ionică Bizău

Reputation: 113365

Ignore all fields (keys) into Mongo find

From documentation find() is defined like bellow:

db.collection.find(query, projection)

where the projection is an object that specifies the fields to return using projection operators.

To return all fields in the matching document, omit this parameter.

My question is that if there is any way to ignore all fields using projection object.

I want to receive a response like this:

[{}, {}, {}, {}]

(an array with empty objects)

To ignore a key I use: {"key": 0}. I've already tried {"$all": 0} that seems that doesn't work.

How can I ignore all fields?


I know that I can use count() function, but this doesn't help me in this case.

Upvotes: 1

Views: 2047

Answers (1)

Calin
Calin

Reputation: 151

To ignore all fields, out of _id, you could use:

db.collection.find(query, {$all: 1})

That will return:

{ "_id" : ObjectId("4d6bf563c2dbe2c5f220dc70") }

{ "_id" : ObjectId("4d6bf563c2dbe2c5f220dc71") }

{ "_id" : ObjectId("4d6bf563c2dbe2c5f220dc72") }

And to remove _id use:

db.collection.find(query, {$all: 1, '_id': 0})

To return:

{ } { } { }

Upvotes: 3

Related Questions