Search code examples
mongodbmongodb-compass

Query all username fields in MongoDB


I know this is a very basic question but I have done hours of research and I can't find it anywhere so maybe it's also useful for other people.

I just downloaded MongoDB Compass because I'm trying to export all username from my account collection and there's no way I can create a query that returns only the username field.

How can I, with MongoDB, get all the values of a specific field?

Thank you


Solution

  • Use Projection.

    Where you can add or remove Field in results of data.

    Try

    db.collection.find({}, { user_name : 1})

    Where 1 is true and 0 is false.