From my MongoDB I want the equivalent for
SELECT column1, column2
FROM tbl
With this code I get all the 'rows' but also all the 'columns'
DBCollection collection = database.getCollection("names");
DBCursor cursor = collection.find();
I for example would like all the 'rows' but only the 'columns': id, name, age
How would I do this?
Thanks for any help!!
Thanks!, I fixed it with the code below.
Above code gives me all the records, with only the column names as above.
The first argument to find (the predicate) is your selection criteria, e.g.
The second limits the fields you retrieve, so for the names over everyone 21 or older:
The field selector always pulls back _id unless you specifically turn it off:
However, Mongo will not check for field existence by default. If you want to select certain fields, and match only results that have those fields, you will want to use:
The MongoDB website has a more detailed description of the .find() function!
If you want select one or multiple columns like SQL query. for example if your SQL query like this
MongoDB query: