I'm trying to get just one field back for all my documents.
I'm new to mongoDB but I can't understand why this isn't working.
var docs = db.collection("articles").find({}, { _id: 0, title:1}).asArray();
I have this query which returns ALL fields, despite putting in the projection of wanting only the title field. The query runs without any errors. Maybe I'm missing something very obvious and need a second pair of eyes to spot it.
Any help is appreciated!
Note: I'm using the mongoDB Atlas' Stitch API.
I'm guessing that you're using MongoDB Stitch Browser SDK (currently version 4).
In this case, the
collectionis an instance of RemoteMongoCollection. Where find() accepts options in RemoteFindOptions format. You can define aprojectionto limits the fields of the matching documents by defining an object withprojectionkey.For example: