I am having a mongodb database from which I am trying to project some data. When I am using the following query in the mongo command line -
db.gholson.find({}, { "_id": 1 }).skip(85).limit(2).pretty();
I am getting the following output:-
{
"_id": ObjectId("5abbde863f10ebda405b91f0"),
"_id": ObjectId("5abbde863f10ebda405b91f1")
}
which is the desired output, but when I set up an express server and use the following query with MongoClient, I am getting all the fields instead of just the ids
.
db.collection('gholson').find( {}, { "_id": 1 }).skip(85).limit(2).toArray(( err, result ) => {
if( err ) throw err;
console.log( result );
callback();
});
The above query returns all the key-value pairs instead of just id. I am unable to figure out, why is this happening.
Since version 3.0 of the driver the second argument of the find
function represents options
, not the projection. Try the following:
db.collection("gholson").find({}, { "projection": { "_id": 1 } })
or
db.collection("gholson").find({}).project({ "_id": 1 })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.